Home > Article > Backend Development > What are the steps to upload php files?
When we develop websites, we often encounter the need to create a file upload function. Below we will introduce you to the detailed steps of creating a file upload function in PHP.
Recommended tutorial: PHP video tutorial
Step 1: Create a file to upload Form
Allowing users to upload files from a form is very useful.
Please look at the following 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 attribute of the ff9c23ada1bcecdd1a0fb5d5a0f18437 tag specifies the What content type to use when submitting the form. Use "multipart/form-data" when your form requires binary data, such as file content.
The type="file" attribute of the d5fd7aea971a85678ba271703566ebfd 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.
Comment: Allowing users to upload files is a huge security risk. Please allow only trusted users to perform file upload operations.
Step 2: Create an upload script
The "upload_file.php" file contains the 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 the client computer to the 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 file being uploaded
●$_FILES["file"]["type"] - The name of the file being uploaded Type of uploaded file
●$_FILES["file"]["size"] - The size of the uploaded file, in bytes
●$_FILES["file"][" tmp_name"] - The name of the temporary copy of the file stored on the server
●$_FILES["file"]["error"] - The error code caused by the file upload
This is a A very simple way to upload files. For security reasons, you should add restrictions on who has permission to upload files.
Step 3: Upload restrictions
In this script, we add restrictions on file uploads. Users can only upload .gif or .jpeg files, and the file size must be less than 20 kb:
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/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"; } ?>
Comments: For IE, the type of jpg file recognized must be pjpeg, for FireFox, it must be jpeg.
Step 4: Save the uploaded file
The above example creates an uploaded file in the PHP temporary folder of the server. Temporary copy.
This temporary copied file will disappear when the script ends. To save the uploaded file, we need to copy it to another location:
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/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 whether the file already exists. If it does not exist, it copies the file to the specified folder.
Comments: This example saves the file to a new folder named "upload".
The above is the detailed content of What are the steps to upload php files?. For more information, please follow other related articles on the PHP Chinese website!