Through PHP, you can upload files to the server.
Create a File upload form
It is useful to allow users to upload files from a form.
Take a look at this HTML form for uploading files:
<html> <body> <form action= "upload_file.php" method= "post" enctype= "Multipart/form-data" > < Label for= "File" >Filename:</label> <input type= "file" name= "file" id= "file"/> <br/> <input Type= "Submit" name= "submit" value= "Submit"/> </form> </body> </html>
Please note the following information about this form:
The Enctype property of the <form> label stipulates what type of content to use when submitting the form. Use "Multipart/form-data" when the form requires binary data, such as the contents of the file.
The type= "file" attribute of the <input> label stipulates that input should be treated as a file. For example, when you preview in a browser, you see a navigation button next to the input box.
Note: Allowing users to upload files is a huge security risk. Only allow trusted users to perform file upload operations.
Create upload Script
The "upload_file.php" file contains code for uploading files:
<?php if ($_files["file"] ["error"] > 0) {echo ' ERROR: '. $_files["File" ["Error"]. "<br/>"; else {echo "Upload:". $_files["File" ["Name"]. "<br/>"; echo "Type:". $_files["File" ["type"]. "<br/>"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb<br/>"; echo "Stored in:". $_files["File" ["Tmp_name"]; }?>
By using PHP's global array $_files, you can upload files from a client computer to a remote server.
The first parameter is the input name of the form, and the second subscript can be "name", "type", "Size", "tmp_name", or "error". Like this:
$_files["File" ["Name"]-the name of the uploaded file $_files["file" ["type"]-the type of file being uploaded $_files["file" ["Size"]-the size of the file being uploaded, in bytes $_ files["File" ["Tmp_name"]-the name of the temporary copy of the file stored on the server $_files["file" ["Error"]-error code caused by file upload
This is a very simple way to upload files. For security reasons, you should increase the restrictions on what users have permission to upload files.
Upload Limit
In this script, we added restrictions on file uploads. Users can only upload. gif or. jpeg files, the file size must be less than KB:
<?php if (($_files["File" ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "jpeg") | | ($_files["File"] ["type"] = = "Image/pjpeg") && ($_files["file"] ["size"] < 20000) {if ($_files["file"] ["error"] > 0) {echo ' ERROR: '. $_files["File" ["Error"]. "<br/>"; else {echo "Upload:". $_files["File" ["Name"]. "<br/>"; echo "Type:". $_files["File" ["type"]. "<br/>"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb<br/>"; echo "Stored in:". $_files["File" ["Tmp_name"]; } else {echo "Invalid file";}?>
Note: For IE, the type of recognition JPG file must be pjpeg, for FireFox, must be JPEG.
Save uploaded files
The above example creates a temporary copy of the uploaded file in the server's PHP Temp folder.
This temporary copy file disappears at the end of the script. To save the uploaded file, we need to copy it to another location:
<?php if (($_files["File" ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "jpeg") | | ($_files["File"] ["type"] = = "Image/pjpeg") && ($_files["file"] ["size"] < 20000) {if ($_files["file"] ["error"] > 0) {echo "Return Code:". $_files["File" ["Error"]. "<br/>"; else {echo "Upload:". $_files["File" ["Name"]. "<br/>"; echo "Type:". $_files["File" ["type"]. "<br/>"; echo "Size:". ($_files["File"] ["size"]/1024). "Kb<br/>"; echo "Temp file:". $_files["File" ["Tmp_name"]. "<br/>"; if (file_exists ("upload/". $_files["File" ["name"]) {echo $_files["file"] ["name"]. "already exists." else {move_uploaded_file ($_files["file"] ["Tmp_name"], "upload/". $_files["File" ["name"]); echo "Stored in:". " Upload/". $_files["File" ["Name"]; }} else {echo "Invalid file";}?>
The above script detects if the file already exists, and if it does not, copies the file to the specified folder.
Note: This example saves the file to a new folder named "Upload".