Using Cainiao's eyes to talk about uploading files in php. I want to write a discuz plug-in. this plug-in function involves the file upload function, so I learned how to upload files in php from the perspective of Cainiao. First, w3cshool checked the case. I wanted to write a discuz plug-in. this plug-in function involves the file upload function, so I learned how to upload files in php from the perspective of Cainiao.
First of all, w3cshool checked the case and thought that he was very detailed. I even knew a little about this Cainiao.
Pasted address: http://www.w3school.com.cn/php/php_file_upload.asp
According to this explanation, I wrote his demo and pasted the code:
Html:
This single table page, as a php rookie, describes what I learned in this page:
1. the form attribute enctype, which Baidu translates to be known, is the abbreviation of encode type, which specifies the encoding format for transmitting information to the server;
2. the type attribute file of input, which is uploaded by the dedicated file;
Php:
// Echo phpinfo ();
// Var_dump ($ _ FILES); die;
If ($ _ FILES ["file"] ["type"] = "image/gif ") | ($ _ FILES ["file"] ["type"] = "image/jpeg ") | ($ _ FILES ["file"] ["type"] = "image/pjpeg ")) & ($ _ FILES ["file"] ["size"] <100*1024*1024 )){
If ($ _ FILES ["file"] ["error"]> 0 ){
Echo "Error:". $ _ FILES ["file"] ["error"]."
";
} Else {
Echo "Upload:". $ _ FILES ["file"] ["name"]."
";
Echo "Type:". $ _ FILES ["file"] ["type"]."
";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "Kb
";
Echo "Stored in". $ _ FILES ["file"] ["tmp_name"];
}
If (file_exists ("upload/". $ _ FILES ["name"] ["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 ";
}
When debugging this demo, I encountered a problem: when running this demo, php reported warning, indicating that the upload was unsuccessful.
At this time, I want to print out the $ _ FILES variable. The result is displayed with error = 1. only then can I know that the uploaded file exceeds php. ini file size, causing Upload failure.
Here we will talk about the new knowledge points:
Common $ _ FILES system functions in PHP programming are used as follows:
$ _ FILES ['myfile'] ['name'] displays the original name of the client file.
$ _ FILES ['myfile'] ['type'] MIME type of the file, for example, "image/gif ".
$ _ FILES ['myfile'] ['size'] size of the uploaded file, in bytes.
$ _ FILES ['myfile'] ['tmp _ name'] indicates the temporary file name, which is generally the default file name.
$ _ FILES ['myfile'] ['error'] error code related to file upload. The meanings of different codes are as follows:
0. the file is uploaded successfully.
1; exceeds the file size set by the system in php. ini.
2. the file size is exceeded.
The value specified by the MAX_FILE_SIZE option.
3. only part of the file is uploaded.
4. no files are uploaded.
5. the size of the uploaded file is 0.
At this point, we should know what the error caused by my running demo is, since it was found to be php. in ini, the limit is exceeded. then I modified php. ini configuration.
To sum up my feelings about modifying the php. ini Upload restrictions:
First, modify the php file size limit. there are two parameters in ini: upload_max_filesize and post_max_size. modify the size of these two parameters!
The second is to find php. ini location, because my local computer is building an integrated environment, so php. ini is in the apache folder. if it is an environment set up by yourself, it will be under the php folder. if it cannot be found, echo phpinfo () to see php. the location of the INI file.
So far, new users like me will be able to run the demo above w3cshool and upload the instance.
For uploading files, I have read the plug-ins developed by the authors of discuz and I am a little bit impressed. I will post them to share with you:
$ FileTypes = array ('mp3', 'wav '); // defines the file types that can be uploaded.
$ Result = null;
$ UploadDir = './mail'; // Upload path
If (! Submitcheck ($ _ POST ['formhash2 ']) {// checks whether a file is uploaded.
If ($ _ POST ['upname'] = '') {// determines whether the name of the uploaded file is null.
$ Result = lang ('In in/saya_mails ', 'noname ');
} Else {
$ Myfile = $ _ FILES ['myfile']; // obtain the information of the uploaded file
$ MyfileType = substr ($ myfile ['name'], strrpos ($ myfile ['name'], ".") + 1); // Get the suffix of the uploaded file
// $ MyfileTyle = substr (strrchr ($ myfile ['name'], '.'), 1 );
If ($ myfile ['size']> 1024*1024*1024) {// determines whether the size of the uploaded file has exceeded the limit.
$ Result = lang ('In in/saya_mails ', 'day ');
} Else if (! In_array ($ myfileType, $ fileTypes) {// determines whether the upload type is allowed.
$ Result = lang ('In in/saya_mails ', 'type ');
} Elseif (is_uploaded_file ($ myfile ['tmp _ name']) {// determines whether the file is uploaded through HTTP post.
$ ToFile = './source/plugin/saya_mails/mail/'. $ myfile ['name']; // target storage address
If (@ move_uploaded_file ($ myfile ['tmp _ name'], $ toFile )) {// copy the file to the target storage address. // add @ to this address to block error messages and warnings.
// If (copy ($ myfile ['tmp _ name'], $ toFile )){
$ End = 0;
$ Result = lang ('In in/saya_mails ', 'success ');
} Else {
$ Result = lang ('In in/saya_mails ', 'unknow ');
}
} Else {
$ Result = lang ('In in/saya_mails ', 'day ');
}
}
}
Compared with the upload instance above w3cshool, I think this author is more perfect.
The general process is:
1. determine whether a file is uploaded. this method is provided by discuz. we generally use this method to determine whether the value of the hidden parameter passed by form does not exist;
2. check whether the name of the uploaded file is empty. skip this step. He wrote an input file himself;
3. determine whether the upload size is exceeded;
4. obtain the file suffix and determine whether the file type is allowed to be uploaded;
5. check whether the file is uploaded through http post;
6. move and save the file;
As for the above process, I have summarized my new knowledge points:
1. for the file suffix, the original plug-in author uses the strrpos () function to return the location where "." is located, and then gets the suffix of the uploaded file by intercepting the substr () function.
Here, the strrpos () function should be short for string return position. of course, I have not verified it! This function returns the final position of the string to be searched and returns the position. That is, from the back to the front, where the first appearance occurs. Reference: http://www.w3school.com.cn/php/func_string_strrpos.asp
The original author used this method to determine whether this method is acceptable. I found that it can also be implemented using the strrchr () and substr () functions, I put the method I want to comment in the above source code inside, in fact, almost, strrchr () function is to return the last appearance of the string to the end of the string, reference address: http://www.w3school.com.cn/php/func_string_strrchr.asp
The above two methods are used to determine whether the type of the file to be uploaded is up to standard, instead of judging by $ _ FILES ["file"] ["type"]. This makes better judgment. for beginners, as long as you print the $ _ FILES parameter, you will know that the type attribute is not so clear and clear.
2. use is_uploaded_file () to determine whether the file was uploaded over http.
3. the @ above move_uploaded_file () is used to block error messages and warnings.
Bytes. First, w3cshool checked the case ,...