Home > Article > Backend Development > How to receive file upload in PHP
In PHP, there is a predefined variable $_files that is specially used to store files uploaded by users.
"upload_file.php" The file contains the code for uploading files: (recommended learning: PHP Video Tutorial)
<?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 uploaded file
$_FILES["file"]["type"] - The uploaded file Type
$_FILES["file"]["size"] - The size of the uploaded file, in bytes
$_FILES["file"]["tmp_name"] - The name of a temporary copy of the file stored on the server
$_FILES["file"]["error"] - error code caused by a file upload
This is a very simple file upload Way. For security reasons, you should add restrictions on who has permission to upload files.
The above is the detailed content of How to receive file upload in PHP. For more information, please follow other related articles on the PHP Chinese website!