Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PHP uploads files to the server

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report