Get the App
SLTechnology News&Howtos  ›  Development  › 

How PHP uploads files to the server

Shulou Source: shulou.com Published: 2022-06-01 13:12:45 09月10日 Update

This article will explain in detail how PHP uploads files to the server. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, create a file upload form:

The enctype attribute of the tag specifies which content type to use when submitting the form, and use "multipart/form-data" when the form requires binary data, such as file content.

The type= "file" attribute of the tag specifies that the input should be treated as a file, for example, when previewing in a browser, you will see a browse button next to the input box, for example:

The file selection window opens to filter out pictures in four formats: png, jpg, gif and bmp.

Please note: when uploading pictures, ie browsers: jpg and jpeg are translated into image/pjpeg,png and image/x-png.

While Google and Firefox are very standard: jpg and jpeg are translated into image/jpeg,png, translated into image/png,gif, translated into image/gif,bmp and translated into image/bmp.

Filename:

Use the PHP global array $_ FILES to upload files from the client computer to the remote server:

If ($_ FILES ['file'] [' error'] > 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'] } / * * $_ FILES ["file"] ["name"]-name of the uploaded file * $_ FILES ["file"] ["type"]-Type of the uploaded file * $_ 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 * /

3. Add restrictions on file upload: for example, users can only upload gif or jpeg files and the file size must be less than 20kb:

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"]. 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";}

Save the uploaded file: check whether the file already exists in the specified folder, and if not, copy the temporary copy of the uploaded file to the specified folder:

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"]. "

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";} this is the end of the article on "how PHP uploads files to the server". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Files servers services content articles forms browsing copies names pictures sizes properties folders more tags browsers types input good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information MySQL Docker Shulou Technology