Click here to Skip to main content
16,013,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi pple

I have an question, i am creating a webpage for an assignment and one is that I have to create a page for the users to upload their pictures for other to see and take but I would like to know how do i go about doing that in php. can someone point me in the right direction. or at least give me a head start.


What I have tried:

I have an question, i am creating a webpage for an assignment and one is that I have to create a page for the users to upload their pictures for other to see and take but I would like to know how do i go about doing that in php. can someone point me in the right direction. or at least give me a head start.
Posted
Updated 10-May-18 3:02am

1 solution

I hope it will help.


<form action="accept-file.php" method="post" enctype="multipart/form-data">
Your Photo: <input type="file" name="image" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
After you create this HTML form, you’ll now have to write a PHP script in order to process the PHP file upload. Below you’ll find PHP file upload script code for the same.
PHP Script:
//if they DID upload a file...
if($_FILES['image']['name'])
{
//if no errors...
if(!$_FILES['image']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['image']['tmp_name']); //rename file
if($_FILES['image']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}

//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/'.$new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['image']['error'];
}
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900