Php file upload form learning notes php file Upload
With PHP, you can upload files to the server.
Bytes -------------------------------------------------------------------------------------------------------------------
Create a file upload form: it is very useful for users to upload files from the form;
The following is an html form for uploading files:
The enctype attribute of a tag 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 ".
The type = "file" attribute of the 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 perform operations on files.
Bytes -------------------------------------------------------------------------------------------------------------------
Create an upload script:
The "upload_file.php" file contains the code for uploading files:
0) {echo "Upload Error: ". $_FILES["file"]["error"] . "
";}else {echo "Upload : ". $_FILES["file"]["name"] . "
";echo "Type : ". $_FILES["file"]["type"] . "
";echo "Size : ". $_FILES["file"]["size"]/1024 . " kb
";echo "Store in : ". $_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 table can be: "name", "type", "size", "tmp_name" or "error". just like this:
$ _ FILES ["file"] ["name"]-name of the uploaded file
$ _ FILES ["file"] ["type"]-type of the file to be uploaded
$ _ 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. Based on security considerations, you should add restrictions on which users have the right to upload files.
In this script, we have added restrictions on file upload. You can only upload. gif or. jpeg files. the file size must be smaller than 20 kb:
0) {echo "Error: " . $_FILES["file"]["error"] . "
";}else {echo "Upload: " . $_FILES["file"]["name"] . "
";echo "Type: " . $_FILES["file"]["type"] . "
";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";echo "Stored in: " . $_FILES["file"]["tmp_name"];}}else {echo "Invalid file";}?>
Note: for IE, the jpg file type must be pjpeg and for FireFox, it must be jpeg.
0) {echo "Return Code: " . $_FILES["file"]["error"] . "
";}else {echo "Upload: " . $_FILES["file"]["name"] . "
";echo "Type: " . $_FILES["file"]["type"] . "
";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";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 checks whether the file already exists. if it does not exist, copy the file to the specified folder.
Note: In this example, the file is saved to a new folder named "upload.