With PHP, it is possible to upload files to the server.
To allow users to upload files from a form can be very useful.
Look at the following HTML form for uploading files:
Notice the following about the HTML form above:
Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads.
The "upload_file.html" file contains the code for uploading a file:
By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:
A very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.
In this script we add some restrictions to the file upload. The user may only upload .png or .jpeg files and the file size must be under 50 kb:
Note: For IE to recognize jpg files the type must be pjpeg, for FireFox it must be jpeg.
The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.
The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:
The script above checks if the file already exists, if it does not, it copies the file to the specified folder.
Note: This example saves the file to a new folder called "upload"
Your Query was successfully sent!