Using Cainiao's eyes to talk about uploading files in php
Source: Internet
Author: User
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 of all, w3cshool checked the case and thought that he was very detailed. I even knew a little about this Cainiao. Paste address: http: // www. w3... "/> <scripttype =" text/javascript "src =" ht 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 of all, w3cshool checked the case and thought that he was very detailed. I even knew a little about this Cainiao. Paste address: http://www.w3school.com.cn/php/php_file_upload.asp according to this explanation, wrote his demo, paste code: html: This table single page, as a php rookie I said I learned in this new thing: 1. the form attribute enctype, which Baidu translates to be known. this is the abbreviation of encode type, which specifies the encoding format for transmitting information to the server; 2. input type attribute file, which is uploaded by the dedicated file; php: Copy code // 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 "]."
& Quot; echo & quot; Size: & quot;. ($ _ FILES [& quot; file & quot;] [& quot; size & quot;]/1024). & quot; 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 copying code about debugging this demo, I encountered a problem: when running this demo, php reports a warning, indicating that the upload is not successful. 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 I will talk about some new knowledge points: Common $ _ FILES system functions in the PHP programming language used to copy code: $ _ 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 following code indicates that the file is successfully uploaded. 1; exceeds the file size set by the system in php. ini. 2. the file size exceeds 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. Copy the code here and you will know what caused the error I just ran the demo, since it was found to be php. in ini, the limit is exceeded. then I modified php. ini configuration. To sum up, I modified this 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. I have read the plug-ins developed by other plug-ins of discuz. I am a little bit impressed. I will share with you the following: Copy the code $ fileTypes = array ('mp3 ', 'wav '); // defines the file type that can be uploaded. $ result = null; $ uploadDir = '. /mail'; // Upload path if (! Submitcheck ($ _ POST ['formhash2 ']) {// check whether the object is being 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 uploaded file information $ 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 exceeds the limit $ result = lang (' Plugin/saya_mails ', 'Big');} else if (! In_array ($ myfileType, $ fileTypes) {// Determine 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') ;}} copied the code and compared the upload instance on w3cshool, I think the general process written by this author is: 1. this method is provided by discuz to determine whether a file is uploaded. we generally use this method to determine whether the hidden parameter value passed by form does not exist. 2. you can skip this step to determine whether the name of the uploaded file is null. this is because he wrote an input file. 3. determine whether the upload size exceeds. 4. obtain the file suffix and determine whether the file type is allowed to be uploaded. 5. determine whether the file is uploaded through http post. 6. move and save files. for the above process, I summarized my new information. Knowledge Point: 1. the original plug-in author returns the extension name through the strrpos () function ". "location, and then obtain the suffix of the uploaded file by intercepting the function substr. 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 address: 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 have commented out the method I want in the source code above. In fact, the strrchr () function is to return the string to the end of the last occurrence. refer: 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. the is_uploaded_file () method is used to determine whether the file is uploaded over http. 3. the @ in front of move_uploaded_file () is used to block error messages and warnings.
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