PHP File Upload
It is easy to upload files to the server with PHP
Configure The "php.ini" File
In "php.ini" file, search for the file_uploads directive, and set it to On:
file_uploads = On
Create The HTML Form
Create an HTML form that allow users to choose the image file they want to upload:
Example - create form
Create The Upload File PHP Script
The "upload.php" file contains the code for uploading a file:
Example - file upload
Check if File Already Exists
Add some restrictions. First, check if the file already exists in the "uploads" folder. If it does, an error message is displayed, and $uploadOk is set to 0:
Example - check file exists
Limit File Size
The file input field in our HTML form above is named "fileToUpload".
Check the size of the file. If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0:
Example - check file size
Limit File Type
The code below only allows users to upload JPG, JPEG, PNG, and GIF files. All other file types gives an error message before setting $uploadOk to 0: