Talking about PHP uploading files with rookie's eyes
Source: Internet
Author: User
I want to write a Discuz plug-in, this plug-in function involves uploading file This function, so the Novice's vision to learn the next PHP upload files. First, W3cshool checked the case, think he said very detailed, even I this rookie have a little understand one or two. Post address: http://www.w3school.com.cn/php/php_file_upload.asp Follow this explanation, wrote his demo, put code: HTML: <form action= "demo.php" method= "post" enctype= "Multipart/form-data" > <label for= "File" > filename:</label> <input type= "file" name= "file" id= "file" > <br/> &N Bsp <input type= "Submit" name= "submit" value= "Submit"/> </form> This form page, as a beginner of PHP I said I learned something new in this: 1. form of the property enctype, Baidu translated the word, just know, this is encode type abbreviation, is to specify to the server to pass the information encoding format; 2.input type attribute file, uploaded by this special document; PHP: Replication Code//echo phpinfo (); Var_dump ($_files);d ie; 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) { &NBsp echo "error:". $_files["File" ["Error"]. " <br/> "; }else{ "Upload:". $_files["File" ["Name"]. " <br/> "; echo "type:". $_files["File" ["Type"]. " <br/> "; echo "Size:". ($_files["File"] ["size"]/1024). " Kb<br/> "; echo "Stored in" $_files["file" ["Tmp_name"]; } if (file_exists ("upload/". $_files["Name"] ["name"]) { & nbsp 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"; Copy code about debugging this demo, I encountered a problem: is to run this demo when PHP quoted warning, indicating that the upload was unsuccessful. This time I would like to print out $_files this variable to see, the results of printing to find error=1;Hp.ini upload file size, resulting in upload failure. Here Next I know the new knowledge Points: Copy code Common $_files system functions used in PHP programming languages are: $_files[' myFile ' [' name '] Displays the original name of the client file. $_files[the MIME type of the ' myFile ' [' type '] file, such as "Image/gif". $_files[' myFile ' [' size '] the size of the uploaded file, in bytes. $_files[' myFile ' [' tmp_name '] stores temporary file names, typically the system defaults. $_files[' myFile ' [' ERROR '] The file uploads the associated error code. The following is the meaning of the different code representatives: 0; File uploaded successfully. 1; exceeds the size of the file size php.ini in the system setting. 2; The value specified for the File size max_file_size option is exceeded. 3; The file is only partially uploaded. 4; No files were uploaded. 5; The upload file size is 0. Copy code Here, you should know what I just ran the demo error is what caused, that since the discovery is php.ini inside the limit beyond, then I modified the next php.ini configuration. Summary I modify this php.ini upload restrictions feel: First, to modify the PHP upload file size limit, that to change the php.ini inside two parameters, one is upload_max_filesize, there is a post_max_size, The size of these two parameters can be changed! Second, is to find the location of the php.ini, I because the local computer is built an integrated environment, so php.ini under the Apache folder, if it is their own environment, that is under the PHP folder, if not found, echo under the Phpinfo (), You can see the location of the php.ini file. So far, with my similar novice can run W3cshool above demo, complete upload instance. About uploading files, I looked under the Discuz other Plug-ins author developed Plug-ins, a little harvest, posted to share with you: Copy code $fileTypes = Array (' MP3 ', ' wav '); //defines the file types that are allowed to be uploaded $resulT = NULL; $uploadDir = './mail '; //upload path if (!submitcheck ($_post[' Formhash2 ')) { //detect upload file & nbsp if ($_post[' upname ']== ') { //to determine if the name of the uploaded file is null $result =la Ng (' plugin/saya_mails ', ' noname '); }else{ $myfile = $_files[' myfile ']; &n Bsp //get uploaded file information $myfileType = substr ($myfile [' name '], Strrpos ($m yfile[' name '], "." + 1); //Two get upload file suffix name // $myfileTyle = substr (STRRCHR ($myfile [' name '], '. '), 1; if ($myfile [' Size '] > 1024*1024*1024) { //to determine upload file size Whether to exceed the limit $result = lang (' Plugin/saya_mails ', 'Big '); else if (!in_array ($myfileType, $fileTypes)) { //judgment Is the type allowed to upload $result = lang (' plugin/saya_mails ', ' type '); ElseIf (Is_uploaded_file ($myfile [' tmp_name ']) { // Determine if the file is uploaded via HTTP post $toFile = './source/plugin/say a_mails/mail/'. $myfile [' name ']; //target storage address if (@move_upload Ed_file ($myfile [' Tmp_name '], $toFile)) { //copy the file to the destination store address //This place plus @ is shielding error messages and warnings/ &N Bsp if (copy ($myfile [' Tmp_name '], $toFile)) { &NBSP ; $end = 0; &NBSp $result = lang (' plugin/saya_mails ', ' success '); } else { & nbsp $result = lang (' plugin/saya_mails ', ' unknow '); & nbsp else { $result = lang (' plugin/saya_mails ', ' big '); {}} Copy code In contrast, w3cshool above upload instance, think this author writes more Perfect a little The overall process is: 1. To determine whether the upload file, he used this method is discuz, we generally use, is the form passed over the value of the hidden parameters do not exist to judge; 2. To determine whether the name of the uploaded file is empty, this step can be skipped, This is his own write an input only; 3. To determine whether the upload size is exceeded; 4. Get the file suffix name to determine whether the upload file type is allowed; 5. Determine if the file was uploaded via an HTTP post; 6. Move Save the file; regarding the above process, the individual summarizes the new knowledge points that he obtains: 1. With regard to obtaining the suffix name of the file, the original plugin author returns "By the function Strrpos ()"." Location, and then use the Intercept function substr () to obtain the suffix of the uploaded file. Here, the Strrpos () function, my own understanding should be string return position abbreviation, of course, I have not verified! This function returns the last occurrence of the string to be found in the string, and returns the position. That is, from the back, the first appearance of the position. Reference address: http://www.w3school.com.cn/php/func_string_strrpos.asp Here the original author used this method to judge, it is certainly possible, I Baidu, Discovery can also use STRRCHR () and substr () function to implement this method, I think the method annotation in the above source code, in fact, the STRRCHR () function is to return the last occurrence of the string to find to the end of the string, reference address: http:/ /www.w3school.com.cn/php/func_string_strrchr.asp Use the above two methods to determine whether the type of upload file is up to standard, rather than through $_files["file" ["Type"] To judge, this is better to judge the point, for beginners, because as long as you print the $_files this parameter you know, the type attribute is not so judged clear. 2. By Is_uploaded_file () to determine whether the file is uploaded via http 3.move_uploaded_file () before the @ 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.