This article mainly introduces the PHP limit upload file types and save the upload file method, involving PHP for uploading files of the common operation skills, very practical value, the need for friends can refer to the
-->
The example in this article describes how PHP restricts uploading file types and saves uploaded files. Share to everyone for your reference. Specifically as follows:
The following code shows how to get the user to upload files in PHP, and limit the file type of general picture files, and finally save to the server
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36-37 |
<?php $allowedExts = Array ("GIF", "JPEG", "JPG", "PNG"); $extension = End (Explode (".", $_files["file"] ["name"]); if (($_files["File" ["type"] = = "Image/gif") | | ($_files["File"] ["type"] = = "Image/jpeg") | | ($_files["File"] ["type"] = = "Image/jpg") | | ($_files["File"] ["type"] = = "Image/pjpeg") | | ($_files["File"] ["type"] = = "Image/x-png") | | ($_files["File"] ["type"] = = "Image/png") && ($_files["file"] ["size"] < 20000) && In_array ($extension, $allowedExts)) {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";}?> |
I hope this article will help you with your PHP programming.