It is easy to upload a file to the server with PHP
1. First you must configure PHP to allow file upload
In your "php.ini" file, search for the file_uploads directive, and set it to On:
Code :
file_uploads = On
2. Then, you have to create a HTML form that allow users to choose the file or the image they want to upload, using this code
Code File Link: http://ow.ly/xL7w304Zg3v
Notes:
Some rules to follow for the HTML form above:
- Make sure that the form uses method="post"
- The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form
Without the requirements above, the file upload will not work.
Other things to notice:
- The type="file" attribute of the <input> tag shows the input field as a file-select control, with a "Browse" button next to the input control
The form above sends data to a file called "upload.php", which we will create next.
3. Then you will create the upload file PHP script
The "upload.php" file contains the code for uploading a file:
Code File link : http://ow.ly/Rygr304Zgfy
Note: You will need to create a new directory called "uploads" in the directory where "upload.php" file resides. The uploaded files will be saved there.
4. File Already Exists:
First, we will check if the file already exists in the "uploads" folder. If it does, an error message is displayed, and $uploadOk is set to 0 as you can see in this Code File link : http://ow.ly/iV11304ZgpK
5. Limit File Size:
Now, we want to check the size of the file. If the file is larger than 500KB, an error message is displayed, and $uploadOk is set to 0, You can download the Code File here : http://ow.ly/QlTv304ZgDf
6. Limit File Type:
if you want to allow users to upload JPG, JPEG, PNG, and GIF files. All other file types gives an error message before setting $uploadOk to 0:
Code File Link: http://ow.ly/Oio2304ZgLI