PHP File upload instance (with file type restriction) _php tutorial

Source: Internet
Author: User
Tags php file upload save file
This file upload implementation can be used for instance application development, because of the better security restrictions, of course, you can also upload image file type to get a judge.


Today, the next few sites to improve the file upload system, by the way to send something.

Full PHP code, no JS, file type according to the suffix name, non-MIME judgment.

Create a new up.php with the following code:

The code is as follows Copy Code
$uptype =array ("jar", "zip");
Allow file types to be uploaded
$max _file_size=20480000; Upload file size limit, unit byte
$path _parts=pathinfo ($_server[' php_self '); Get current path
$destination _folder= "files/";
Upload file path
$name = "Muxi_". Date ("Y-m-d_h-i-s");
Save file name
if ($_server[' request_method '] = = ' POST ')
{
$file = $_files["Upload_file"];
if (!is_uploaded_file ($file ["Tmp_name"]))
Whether the file exists
{
echo "file does not exist! ";
Exit
}
$torrent = Explode (".", $file ["name"]);
$fileend = End ($torrent);
$fileend = Strtolower ($fileend);
if (!in_array ($fileend, $uptype))
Check the upload file type
{
echo "does not allow uploading this type of file! ";
Exit
}
if ($max _file_size < $file ["size"])
Check File size
{
echo "File is too large to exceed the upload limit! ";
Exit
}
if (!file_exists ($destination _folder))
mkdir ($destination _folder);
$filename = $file ["Tmp_name"];
$image _size = getimagesize ($filename);
$pinfo =pathinfo ($file ["name"]);
$ftype = $pinfo [extension];
$destination = $destination _folder. $name. ".". $ftype;
if (file_exists ($destination) && $overwrite! = True)
{
echo "file with the same name already exists! ";
Exit
}
if (!move_uploaded_file ($filename, $destination))
{
echo "Error moving File! ";
Exit
}
$pinfo =pathinfo ($destination);
$fname = $pinfo [basename];
echo "uploaded successfully! ";
}
?>

Calling code:

The code is as follows Copy Code




With the MIME type limit has the limitation, some files in the upload is not the normal itself mime, causes the upload to be unsuccessful, but with the suffix name restriction can solve this problem very well.

http://www.bkjia.com/PHPjc/632755.html www.bkjia.com true http://www.bkjia.com/PHPjc/632755.html techarticle This file upload implementation can be used for instance application development, because of the better security restrictions, of course, you can also upload image file type to get a judge. Change Today ...

  • Related Article

    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.