Php file Upload instance (with file type restrictions)

Source: Internet
Author: User
Tags file upload php code php file upload


Today, I improved my file Upload systems for several other websites.

Php code, no js, file type determined by suffix name, non-mime judgment.

Create up. php with the following code:

The code is as follows: Copy code
<? Php
$ Uptype = array ("jar", "zip ");
// Allowed file types
$ Max_file_size = 20480000; // size limit of uploaded files, in bytes
$ Path_parts = pathinfo ($ _ SERVER ['php _ SELF ']); // Obtain the current path
$ Destination_folder = "files /";
// Upload file path
$ Name = "MuXi _". date ("Y-m-d_H-i-s ");
// Save the file name
If ($ _ SERVER ['request _ method'] = 'post ')
{
$ File = $ _ FILES ["upload_file"];
If (! Is_uploaded_file ($ file ["tmp_name"])
// Whether a file exists
{
Echo "the 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 "this type of file cannot be uploaded! ";
Exit;
}
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "the 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 "the file with the same name already exists! ";
Exit;
}
If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "an error occurred while moving the file! ";
Exit;
}
$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
Echo "Upload successful! ";
}
?>

Call code:

The code is as follows: Copy code


<Form action = "up. php" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "upload_file"/>
<Input type = "submit" value = "upload"/>

Restrictions on the use of mime types are limited. If some files are uploaded normally or not, the upload may fail. However, the suffix restriction can be used to solve this problem.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.