PHP limit upload file type and save upload file method _php tips

Source: Internet
Author: User
Tags php programming

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

<?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"] ["E Rror "] > 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.