PHP uploading images to the server

Source: Internet
Author: User
Tags php file upload php programming language

The $_files system function is used in the PHP file crossing.

First, $_files system functions

Common $_files system function usages in the PHP programming language 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 '] The temporary file name stored, usually the system default.

Second, common functions

Common $_files system function usages in the PHP programming language 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 '] 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. Third, php file upload

To upload a file through a form, you first need to mark <form action= "setting.php" enctype= "Multipart/form-data" method= "POST" >. The following HTML code:

<form action= "setting.php" enctype= "Multipart/form-data" method= "POST" > <div class= "left" > Merchant Shop Name: & lt;br/> <input name= "Sjname" class= "input1" type= "text" ><br/> contact phone:<br/> <input Name = "Sjphone" class= "input1" type= "text" ><br/>QQ:<br/> <input name= "sjqq" class= "input1" type= "text "><br/> slogan:<br/> <input name=" Sjad "class=" input1 "type=" text "placeholder=" 20 words or less ">& lt;br/> </div> <div class= "right" > Upload cover Map: (Recommended 320*120) <br/> <input type= "Tex T "readonly=" readonly "name=" file_name "id=" file_name "class=" Fileinput "/><input type=" file "Name=" Img_file " Style= "Display:none" onchange= "File_change (this.value)"/><input type= "button" value= "Browse onclick=" Img_ File.Click (); "Name=" Get_file "class=" Liulan "/><br/> Merchant profile:<br/> <textarea name=" sjbrief "class=" Te Xtarea "placeholder=" within 100 words "></textarea> </div> <div class= "clear" > </div> <p> <input class= "buttons Ave "Type=" submit "value=" Save Settings "/>|<a style= ' text-decoration:none; ' href=" binding.php?ac=del "target =" _top " ><input class= "buttonquit" type= "button" value= "unbind AP"/></a> </p> </form>

The PHP code is as follows:

<?php/** * Created by Phpstorm. * User:funmi * date:14-9-10 * Time: PM 6:57 */include_once ('./common.php '); if ($_sglobal[' login ']==true) {$cover _pic =    Upload_store_pic ();    $store _name = $_post[' sjname ');    $tel _number = $_post[' Sjphone ');    $ad _words = $_post[' Sjad ');    $store _introduce = $_post[' sjbrief ');    $QQ _number = $_post[' sjqq '); $setarr =array (' cover_pic ' = $cover _pic, ' store_name ' + $store _name, ' tel_number ' = $tel _number    , ' ad_words ' = $ad _words, ' store_introduce ' and $store _introduce, ' qq_number ' + $qq _number); Updatetable (Tname (' Open_member_weixin_ap '), $setarr, Array (' Apid ' =>$_cookie[' apid '], ' id ' =>$_cookie[' id '))    ;    $smarty->assign (' Loginuser ', $_cookie[' loginuser ');    $smarty->display (' setting.dwt '); Exit ();}    Gourl (' binding.php '); function upload_store_pic () {//File save path $save _path = './uploads/store_pic/';    if (!file_exists ($save _path)) {mkdir ($save _path); }//define Allow onThe file extension $ext _arr = array (' image ' = = Array (' gif ', ' jpg ', ' jpeg ', ' PNG ', ' bmp '));    Maximum file size $max _size = 1000000;                 PHP upload failed if (!empty ($_files[' img_file ' [' Error '])) {switch ($_files[' img_file ' [' Error ']) {case ' 1 ': $error = ' exceeds the allowable size of php.ini.                ';            Break Case ' 2 ': $error = ' exceeds the size allowed by the form.                ';            Break Case ' 3 ': $error = ' picture is only partially uploaded.                ';            Break Case ' 4 ': $error = ' Please select the picture.                ';            Break Case ' 6 ': $error = ' cannot find the temp directory.                ';            Break Case ' 7 ': $error = ' ERROR writing file to hard disk.                ';            Break Default: $error = ' Unknown error.        ';        } showmessage ($error);    Return ';        }//have upload file if (empty ($_files) = = = False) {//original file name $file _name = $_files[' img_file ' [' name ']; Temporary file name on server $tmp _name = $_files[' Img_filE ' [' tmp_name '];        File size $file _size = $_files[' img_file ' [' Size ']; Check the filename if (! $file _name) {showmessage ("Please select File.            ");        Return '; }//Check the directory if (@is_dir ($save _path) = = = False) {ShowMessage ("The upload directory does not exist.            ");        Return '; }//Check the directory Write permission if (@is_writable ($save _path) = = = False) {showmessage ("Upload directory does not have write permission.            ");        Return '; }//Check if the IF (@is_uploaded_file ($tmp _name) = = = False) {ShowMessage ("upload failed.            ");        Return '; }//Check file size if ($file _size > $max _size) {showmessage ("Upload file size exceeds limit.            ");        Return '; }//Check the directory name $dir _name = Empty ($_get[' dir ')?        ' Image ': Trim ($_get[' dir '); if (Empty ($ext _arr[$dir _name])) {showmessage ("The directory name is incorrect.            ");        Return ';        }//Get file extension $temp _arr = explode (".", $file _name); $file _ext = Array_pop ($temp _arr);        $file _ext = Trim ($file _ext);        $file _ext = strtolower ($file _ext); Check the extension if (In_array ($file _ext, $ext _arr[$dir _name]) = = = False) {ShowMessage ("The upload file name extension is not allowed.            ");        Return '; }//New file name $new _file_name = Date ("Ymdhis"). '_' . RAND (10000, 99999). ' _apid_ '. $_cookie[' Apid '. '.' .        $file _ext; Move file $file _path = $save _path.        $new _file_name; if (Move_uploaded_file ($tmp _name, $file _path) = = = False) {ShowMessage ("failed to upload the file.            ");        Return ';        } @chmod ($file _path, 0777);    return $file _path;    }else{return '; }}


PHP uploading images to the server

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.