The specific idea and implementation of PHP file upload. Recently, I was very interested in php file processing, so I read many file processing articles on many websites, however, the PHP file processing knowledge on many sites in China has recently become very interested in php file processing. Therefore, I have read many file processing articles on many sites, however, most of the PHP file processing knowledge on many websites in China is that you copy my file, and most of the items searched by baidu.com or google.com are repeated. I recently wrote an article about Alibaba Cloud security on a website outside China, so I suggest you read it.
First, it is necessary to describe the file upload procedure and knowledge points used:
For file upload, we need to use the type = "file" type of the form in HTML and its enctype attribute. This is what we all need. Of course, we must use the FILE function library, string function library, directory function library, and $ _ FILES [] in the PHP function library.
Each website may have many restrictions on the upload of files. These restrictions include the file type, file size, extension, the existence of the upload directory, and the existence of the uploaded file, directory writability and readability, renaming uploaded files, and copying files from the cache to the desired directory.
Of course, error preprocessing cannot be ignored! If we further discuss it, we can use the event log record for file operations.
We can use a program to implement these functions:
Bytes --------------------------------------------------------------------------------------------
The first is our preset variable value, which includes the file size, file extension type, MIMI type, and whether to delete the switch variable.
$ MAX_SIZE = 2000000;
$ FILE_MIMES = array (image/jpeg, image/jpg, image/gif
, Image/png, application/msword );
$ FILE_EXTS = array(.zip,.jpg, .png, .gif );
$ DELETABLE = true;
Next, set the browser access variables and directory access variables:
$ Site_name = $ _ SERVER [HTTP_HOST];
$ Url_dir = "http: //". $ _ SERVER [HTTP_HOST]. dirname ($ _ SERVER [PHP_SELF]);
$ Url_this = "http: //". $ _ SERVER [HTTP_HOST]. $ _ SERVER [PHP_SELF];
$ Upload_dir = "files /";
$ Upload_url = $ url_dir. "/files /";
$ Message = "";
Create an upload directory and change the permission accordingly:
If (! Is_dir ("files ")){
If (! Mkdir ($ upload_dir ))
Die ("upload_files directory doesnt exist and creation failed ");
If (! Chmod ($ upload_dir, 0755 ))
Die ("change permission to 755 failed .");
}
Processing of user requests:
If ($ _ REQUEST [del] & $ DELETABLE ){
$ Resource = fopen ("log.txt", "");
Fwrite ($ resource, date ("Ymd h: I: s"). "DELETE-$ _ SERVER [REMOTE_ADDR]". "$ _ REQUEST [del]");
Fclose ($ resource );
If (strpos ($ _ REQUEST [del], "/.")> 0); // possible hacking
Else if (strpos ($ _ REQUEST [del], "files/") === false); // possible hacking
Else if (substr ($ _ REQUEST [del], 0, 6) = "files /"){
Unlink ($ _ REQUEST [del]);
Print "script window. location. href = $ url_this? Message = deleted successfully script ";
}
}
Else if ($ _ FILES [userfile]) {
$ Resource = fopen ("log.txt", "");
Fwrite ($ resource, date ("Ymd h: I: s"). "UPLOAD-$ _ SERVER [REMOTE_ADDR]"
. $ _ FILES [userfile] [name]. ""
. $ _ FILES [userfile] [type]. "");
Fclose ($ resource );
$ File_type = $ _ FILES [userfile] [type];
$ File_name = $ _ FILES [userfile] [name];
$ File_ext = strtolower (substr ($ file_name, strrpos ($ file_name ,".")));
// Check the file size:
If ($ _ FILES [userfile] [size]> $ MAX_SIZE)
$ Message = "The file size is over 2 MB .";
// File Type/Extension Check
Else if (! In_array ($ file_type, $ FILE_MIMES)
&&! In_array ($ file_ext, $ FILE_EXTS ))
$ Message = "Sorry, $ file_name ($ file_type) is not allowed to be uploaded .";
Else
$ Message = do_upload ($ upload_dir, $ upload_url );
Print "script window. location. href = $ url_this? Message = $ message script ";
}
Else if (! $ _ FILES [userfile]);
Else
$ Message = "Invalid File Specified .";
List uploaded files:
$ Handle = opendir ($ upload_dir );
$ Filelist = "";
While ($ file = readdir ($ handle )){
If (! Is_dir ($ file )&&! Is_link ($ file )){
$ Filelist. = "". $ file ."";
If ($ DELETABLE)
$ Filelist. = "x ";
$ Filelist. ="
". Date (" d-m H: I ", filemtime ($ upload_dir. $ file ))
."";
$ Filelist. ="
";
}
}
Function do_upload ($ upload_dir, $ upload_url ){
$ Temp_name = $ _ FILES [userfile] [tmp_name];
$ File_name = $ _ FILES [userfile] [name];
$ File_name = str_replace ("\", "", $ file_name );
$ File_name = str_replace ("", "", $ file_name );
$ File_path = $ upload_dir. $ file_name;
// File Name Check
If ($ file_name = ""){
$ Message = "Invalid File Name Specified ";
Return $ message;
}
$ Result = move_uploaded_file ($ temp_name, $ file_path );
If (! Chmod ($ file_path, 0777 ))
$ Message = "change permission to 777 failed .";
Else
$ Message = ($ result )? "$ File_name uploaded successfully .":
"Somthing is wrong with uploading a file .";
Return $ message;
}
?>
My Files
Developed
CityPost. ca
...