php File Upload
With 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;
The following is an HTML form for uploading a file:
The enctype attribute of the <form> tag specifies the type of content to be used when the form is submitted. Use "Multipart/form-data" when the form requires binary data, such as the contents of a file.
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, you'll see a browse button next to the input box.
Note: Allowing users to upload files is a huge security risk. Please allow only trusted users to perform actions on the file.
--------------------------------------------------------------------------------------------------------------- ----
To Create an upload script:
The "upload_file.php" file contains the code for uploading the file:
<?php<span style= "White-space:pre" ></span>if ($_files["File" ["error"] > 0) {<span style= " White-space:pre "></span>echo" Upload Error: ". $_files["File" ["Error"]. "<br/>"; <span style= "White-space:pre" ></span>}else {<span style= "White-space:pre" ></ Span>echo "Upload:". $_files["File" ["Name"]. "<br/>"; <span style= "White-space:pre" ></span>echo "Type:". $_files["File" ["type"]. "<br/>"; <span style= "White-space:pre" ></span>echo "Size:". $_files["File" ["Size"]/1024. "Kb<br/>"; <span style= "White-space:pre" ></span>echo "Store in:". $_files["File" ["Tmp_name"]. "<br/>"; <span style= "White-space:pre" ></span>}?>
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 table can be: "Name", "type", "Size", "Tmp_name" or "error". Just like this:
$_files["File" ["Name"]-the name of the file being uploaded
$_files["File" ["type"]-the type of file being uploaded
$_files["File" ["Size"]-the size of the uploaded file, measured 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. Based on security considerations, you should increase the restrictions on what users have permission to upload files.
In this script, we have added restrictions on file uploads. Users can only upload. gif or. jpeg files, and the file size must be less than KB:
<?php<span style= "White-space:pre" ></span>if ((($_files["File" ["type"] = = "Image/gif") <span Style= "White-space:pre" ></span>| | ($_files["File" ["type"] = = "Image/jpeg") <span style= "White-space:pre" ></span>| | ($_files["File" ["type"] = = "Image/pjpeg")) <span style= "White-space:pre" ></span>&& ($_files["File" ["Size"] < 20000)) {<span style=] White-space:pre "></span>if ($_files[" File "[" error "] > 0) {<span style=" White-space:pre "></ Span>echo "Error:". $_files["File" ["Error"]. "<br/>"; <span style= "White-space:pre" ></span>}else {<span style= "White-space:pre" ></ Span>echo "Upload:". $_files["File" ["Name"]. "<br/>"; <span style= "White-space:pre" ></span>echo "Type:". $_files["File" ["type"]. "<br/>"; <span style= "White-space:pre" ></span>echo "Size:". ($_files["File" ["Size"]/1024). "Kb<br/>"; <span style= "White-space:Pre "></span>echo" Stored in: ". $_files["File" ["Tmp_name"];<span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>}else {<span style= "white-space:pre" ></span>echo "Invalid file"; <span style= " White-space:pre "></span>}?>
Note: For IE, the type of the recognized JPG file must be pjpeg, and for FireFox, it must be JPEG.
<?php<span style= "White-space:pre" ></span>if ((($_files["File" ["type"] = = "Image/gif") <span Style= "White-space:pre" ></span>| | ($_files["File" ["type"] = = "Image/jpeg") <span style= "White-space:pre" ></span>| | ($_files["File" ["type"] = = "Image/pjpeg")) <span style= "White-space:pre" ></span>&& ($_files["File" ["Size"] < 20000)) {<span style=] White-space:pre "></span>if ($_files[" File "[" error "] > 0) {<span style=" White-space:pre "></ Span>echo "Return Code:". $_files["File" ["Error"]. "<br/>"; <span style= "White-space:pre" ></span>}else {<span style= "White-space:pre" ></ Span>echo "Upload:". $_files["File" ["Name"]. "<br/>"; <span style= "White-space:pre" ></span>echo "Type:". $_files["File" ["type"]. "<br/>"; <span style= "White-space:pre" ></span>echo "Size:". ($_files["File" ["Size"]/1024). "Kb<br/>"; <span style= "white-Space:pre "></span>echo" Temp file: ". $_files["File" ["Tmp_name"]. "<br/>"; <span style= "White-space:pre" ></span>if (file_exists ("upload/". $_FILES["File" ["Name"] ) {<span style= "white-space:pre" ></span>echo $_files["file" ["Name"]. "already exists."; <span style= "White-space:pre" ></span>}else {<span style= "White-space:pre" ></span>move_ Uploaded_file ($_files["file"] ["Tmp_name"],<span style= "White-space:pre" ></span> "upload/". $_files[" File "[" name "]); <span style=" White-space:pre "></span>echo" Stored in: "." Upload/". $_files["File" ["Name"];<span style= "White-space:pre" ></span>}<span style= "White-space:pre" > </span>}<span style= "White-space:pre" ></span>}else {<span style= "White-space:pre" ></ Span>echo "Invalid file"; <span style= "White-space:pre" ></span>}?>
The script above 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".
PHP File Upload Form ~ ~ Learn Notes