A beginner's eye on PHP uploading files _php Tutorial

Source: Internet
Author: User
Tags php programming language phpinfo
I want to write a discuz plugin, this plug-in function involves uploading files this function, so the Novice's vision to learn the next PHP upload file. First, W3cshool checked the case, I think he said very detailed, even I this rookie have a slight understanding of one or two. Address: Http://www.w3school.com.cn/php/php_file_upload.asp According to this explanation, wrote the next he this demo, paste the code: HTML: This form page, As a novice php I said I learned in this new things: 1.form attribute enctype, Baidu translated the next word, just know, this is the abbreviation of encode type, is to specify the encoding format for transmitting information to the server; The Type property of the 2.input file, This dedicated file is uploaded; PHP: Copy 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) {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";} Copy code about debugging this demo, I encountered a problem: is running this demo when PHP reported warning, indicating that the upload is unsuccessful. This time I want to print out $_files this variable to see, the results print out to find error=1, only to know that the uploaded file exceeds the php.ini upload file size, resulting in upload failure.  Here's my new knowledge point: The common $_files system function usage in the PHP programming language of Copy code is: $_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 '] The temporary file name stored, usually the system default. $_files[' myFile ' [' ERROR '] The file uploads the relevant error code. The following are the meanings represented by different codes: 0;  File upload succeeded. 1;  The size of the system is exceeded in the file size php.ini. 2;  The value specified by the File size max_file_size option is exceeded. 3;  Only part of the file is uploaded. 4;  No files were uploaded. 5; The upload file size is 0. Copy the code here, you should know that I just run the demo error is what caused, since the discovery is php.ini inside the limit exceeded, then I modified the next php.ini configuration. In summary, I modified this php.ini upload limit feeling: First, to modify the PHP upload file size limit, then change the php.ini inside theTwo parameters, one is upload_max_filesize, there is a post_max_size, modify the size of the two parameters can be! Second, is to find the location of the php.ini, I because the local computer is built by the integration environment, so php.ini under the Apache folder, if it is their own environment, it is under the PHP folder, if not found, echo under Phpinfo (), You can see the location of the php.ini file. That's it, and the novice like me will be able to run the demo on the W3cshool and complete the upload example. About uploading files, I looked at the next discuz other plug-ins developed by the plug-in, a little bit of harvest, posted to share with you: Copy code $fileTypes = Array (' MP3 ', ' wav '); Defines the type of file that is allowed to be uploaded $result = null; $uploadDir = './mail '; Upload path if (!submitcheck ($_post[' Formhash2 ')) {//detect if the upload file if ($_post[' upname ']== ') {//Determine if the name of the uploaded file is empty $result =lang (' Plugin/saya_mails ', ' noname '); }else{$myfile = $_files[' myfile ');//Get uploaded file information $myfileType = substr ($myfile [' name '], Strrpos ($myfile [' name '], ".") + 1); Two suffix names for uploading files//$myfileTyle = substr (STRRCHR ($myfile [' name '], '. '), 1); if ($myfile [' Size '] > 1024*1024*1024) {//Determine if the upload file size exceeds the limit $result = Lang (' plugin/saya_mails ', ' big ');} else if (!in_arr Ay ($myfileType, $fileTypes)) {//Determine if the type of upload is allowed $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/saya_mails/mail/'. $myfile [' name ']; Target storage address if (@move_uploaded_file ($myfile [' Tmp_name '], $toFile) {//Copy file to destination storage address//This place plus @ is the mask error message and warning//if (copy ($myfile   [' Tmp_name '], $toFile)) {$end =0; $result = lang (' plugin/saya_mails ', ' success '); } else {$result = lang (' plugin/saya_mails ', ' unknow ');}}    else {$result = lang (' plugin/saya_mails ', ' big ');}   }} Copy code compared to the next, W3cshool above the upload instance, that the author wrote a little more perfect the general process is: 1. Determine whether the upload file, he used this method is discuz, we generally use, is the form passed over the value of hidden parameters exist to judge; 2. Determine whether the name of the uploaded file is empty, this step can be skipped, this is he wrote a input only; 3. Determine if the upload size is exceeded; 4. Gets the file suffix name, determines whether the type of upload file is allowed, 5. Determine if the file was uploaded via HTTP post; 6. Mobile Security Save the file; As for the above process, the individual summarizes the new knowledge points that he has acquired: 1. About getting the suffix name of the file, the original plugin author returns "." Through the function Strrpos ().    Location, and then obtain the suffix of the uploaded file by intercepting the function substr (). Here, the Strrpos () function, my own understanding should be a string return position abbreviation, of course, I have not verified! This function returns the position of the last occurrence of the string to be found in the string, and returns the position. That is, from the back forward, the first occurrence of the position. Reference Address: http://www.w3school.com.cn/php/func_string_strrpos.asp Here The original author use this method to judge, it is certainly possible, I Baidu down, found can also use STRRCHR () and substr () function cooperation to implement this method, I put the method I want to comment on the above source code, in fact, almost, STRRCHR () function is to return the mostThe next occurrence of the string to look up to the end of the string, refer to address: http://www.w3school.com.cn/php/func_string_strrchr.asp The above two methods to determine whether the type of upload file is standard, rather than through $_   files["File" ["type"] to judge, so better to judge the point, for the novice, because as long as you print the $_files this parameter you know, the type attribute is not so clear. 2. Through Is_uploaded_file () to determine whether the file is uploaded via http 3.move_uploaded_file () before the @ is used to block the error message and warning

http://www.bkjia.com/PHPjc/735883.html www.bkjia.com true http://www.bkjia.com/PHPjc/735883.html techarticle I want to write a discuz plugin, this plug-in function involves uploading files this function, so the Novice's vision to learn the next PHP upload file. First of all, W3cshool checked the case, ...

  • Contact Us

    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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.