You can easily upload PHP files,
PHP File Upload
With PHP, you can upload files to the server.
This example is completed in the test project. The directory structure is as follows:
Test
| ----- Upload # File upload directory
| -----Form.html # form File
| ----- Upload_file.php # php Upload code
Source code download: File Upload
Create a file upload form
It is very useful to allow users to upload files from forms.
See the following HTML form for uploading files:
<Html>
Save the preceding code to the form.html file.
Some Considerations for the preceding HTML form are listed as follows:
1. The enctype attribute of <form> label specifies the content type to be used when submitting a form. When a form requires binary data, such as file content, use multipart/form-data ".
2. the type = "file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when previewing in a browser, a browser button is displayed next to the input box.
Note:Allowing users to upload files is a huge security risk. Only trusted users are allowed to upload files.
Create upload script
The "upload_file.php" file contains the code for uploading files:
<? Phpif ($ _ FILES ["file"] ["error"]> 0) {echo "error :". $ _ FILES ["file"] ["error"]. "<br>" ;}else {echo "Upload File Name :". $ _ FILES ["file"] ["name"]. "<br>"; echo "file type :". $ _ FILES ["file"] ["type"]. "<br>"; echo "file size :". ($ _ FILES ["file"] ["size"]/1024 ). "kB <br>"; echo "Location of temporary file storage :". $ _ FILES ["file"] ["tmp_name"] ;}?>
By using the Global Array $ _ FILES of PHP, you can upload FILES from the 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 ". As follows:
$ _ FILES ["file"] ["name"]-Name of the uploaded file
$ _ FILES ["file"] ["type"]-File Upload type
$ _ FILES ["file"] ["size"]-Size of the uploaded file, in bytes
$ _ FILES ["file"] ["tmp_name"]-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 File Upload method. For security considerations, you should add restrictions on which users are allowed to upload files.
Upload restrictions
In this script, we have added restrictions on file upload. You can only upload .gifini.00000000.jpg0000.png files. The file size must be smaller than 200 kB:
<? Php // The image suffix $ allowedExts = array ("gif", "jpeg", "jpg", "png"); $ temp = explode (". ", $ _ FILES [" file "] [" name "]); $ extension = end ($ temp ); // obtain the file suffix if ($ _ FILES ["file"] ["type"] = "image/gif ") | ($ _ FILES ["file"] ["type"] = "image/jpeg ") | ($ _ FILES ["file"] ["type"] = "image/jpg ") | ($ _ FILES ["file"] ["type"] = "image/pjpeg ") | ($ _ FILES ["file"] ["type"] = "image/x-png ") | ($ _ FILES ["file"] ["type"] = "I Mage/png ") & ($ _ FILES [" file "] [" size "] <204800) // smaller than 200 kb & in_array ($ extension, $ allowedExts) {if ($ _ FILES ["file"] ["error"]> 0) {echo "error ::". $ _ FILES ["file"] ["error"]. "<br>" ;}else {echo "Upload File Name :". $ _ FILES ["file"] ["name"]. "<br>"; echo "file type :". $ _ FILES ["file"] ["type"]. "<br>"; echo "file size :". ($ _ FILES ["file"] ["size"]/1024 ). "kB <br>"; echo "Location of temporary file storage :". $ _ FILES ["file"] ["tmp _ Name "] ;}} else {echo" Illegal File Format ";}?>
Save the uploaded file
The above instance creates a temporary copy of the uploaded file in the PHP temporary folder on the server.
The temporary copy file will disappear at the end of the script. To save the uploaded file, we need to copy it to another location:
<? Php // The image suffix $ allowedExts = array ("gif", "jpeg", "jpg", "png"); $ temp = explode (". ", $ _ FILES [" file "] [" name "]); echo $ _ FILES [" file "] [" size "]; $ extension = end ($ temp); // obtain the file suffix if ($ _ FILES ["file"] ["type"] = "image/gif ") | ($ _ FILES ["file"] ["type"] = "image/jpeg ") | ($ _ FILES ["file"] ["type"] = "image/jpg ") | ($ _ FILES ["file"] ["type"] = "image/pjpeg ") | ($ _ FILES ["file"] ["type"] = "image/x-png") | ($ _ FILES ["file"] ["type"] = "image/png ")) & ($ _ FILES ["file"] ["size"] <204800) // smaller than 200 kb & in_array ($ extension, $ allowedExts )) {if ($ _ FILES ["file"] ["error"]> 0) {echo "error ::". $ _ FILES ["file"] ["error"]. "<br>" ;}else {echo "Upload File Name :". $ _ FILES ["file"] ["name"]. "<br>"; echo "file type :". $ _ FILES ["file"] ["type"]. "<br>"; echo "file size :". ($ _ FILES ["file"] ["size"]/1024 ). "kB <br>"; echo "temporary file storage Location :". $ _ FILES ["file"] ["tmp_name"]. "<br>"; // determines whether the file exists in the upload directory of the current directory. // if the file does not exist, you need to create it, the upload directory permission is 777 if (file_exists ("upload /". $ _ FILES ["file"] ["name"]) {echo $ _ FILES ["file"] ["name"]. "The file already exists. ";} Else {// If the upload directory does not exist, upload the file to the upload directory move_uploaded_file ($ _ FILES [" file "] [" tmp_name "], "upload /". $ _ FILES ["file"] ["name"]); echo :". "upload /". $ _ FILES ["file"] ["name"] ;}} else {echo "Illegal file Format" ;}?>
The above script checks whether the file already exists. If it does not exist, copy the file to the directory named "upload.
The file upload demonstration operation is as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.