PHP Tutorial: How To Upload An Image Using Basic PHP

When developing a PHP website, there comes a time when you’re required to add a functionality that allows your users to upload files. This is where the File upload feature comes in handy. This feature allows your users to upload file/files on your server through your PHP website. So, in this PHP tutorial, we’ll cover the basic process of uploading an image using PHP.

Now, before moving ahead to upload image PHP, there are small details that must be correct in order to successfully upload a file. First of all, you need to make sure that PHP is configured to allow uploads. Check your “php.ini” file and ensure the “file_uploads” directive value is “on”.

Files that are uploaded are stored in a temporary directory initially, but you can move these files to a permanent location via your PHP script. For this, you’ll have to specify your location using the “upload_tmp_dir” directive in “php.ini”.

Once, this task is done, it’s time to move on to the coding part. The following HTML form will provide an interface to a user to upload a file.

HTML Form:

<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" />
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 no errors...
//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
//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...
//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:

Want to Develop a Web Application?

Book your free consultation with web app experts.

That’s it! Your work is done here. if you still have any doubts regarding how to upload an image using PHP or mobile app development for PHP image uploads, then you can contact our PHP developers for the solution and if you’d like to hire PHP developer, you can contact Space-O Technologies today.

Do you want to download the whole script so you can use it in your next project? Then you can directly download it on GitHub.

Author Bio
Jigar Mistry
Jigar Mistry
Designation: Vice President (Technologies)

Jigar Mistry is Vice President (Technologies) at Space-O Technologies. He has 14+ years of experience in the web and mobile app development industry. He has expertise in different mobile app categories like health and fitness, eCommerce, and on-demand.