PHP File Upload Example

PHP File Upload is a very important topic in PHP.

Build any kind of websites or web application there has must be files.

So want know that how can upload files or retrieve those files and download it (if required).

You should have basic knowledge of Form Handling in PHP for working uploading a file.

Example: How to upload a file in PHP.

For Uploading a file:

  1. Create a HTML form with method POST and enctype="MULTIPART/FORM-DATA"
  2. Inside form create a input type file <input name="filedetails" type="FILE">
  3. Create a submit button to submit form to server.
  4. HTML Form action="" means file handling work is done by this file(upload.php)
  5. In PHP program check any file uploaded or not $_FILES['filedetails']['name']
  6. Get file and its detail on server.
  7. Save file to specified directory on server   copy($filesource, $destination);
  8. Show appropriate message to user.

upload.php

PHP File Upload Example
Fig: PHP File Upload Example

Showfile.php

Note:
Always use form method post for uploading the file, and enctype is mandatory, otherwise, the file doesn’t upload.

PHP File Upload need to set some options. By default uploaded file size is set 2M.

So sometimes file doesn’t upload if the file size is large than 2M.

That’s why set upload_max_filesize= 500M or any large value in the php.ini file (xampp/php/php.ini).

And also modify the post_max_size value in the php.ini file. By default post_max_size is 8M.

Sometimes for this reason also file doesn’t upload if the size is large then 8M instead of upload_max_size is greater than 8M.

So also set post_max_size=500M or any large value in the php.ini file (xampp/php/php.ini).

Any changes in the php.ini file need to restart XAMPP control panel. Stop apache and start again. Then the updated value will be reflected.

Read More