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.