PHP image Upload

Source: Internet
Author: User
Tags base64 http post

Simple examples of PHP uploads:

HTML file:

<HTML><formAction= "index.php"name= "form"Method= "post"enctype= "multipart/form-data">    <inputtype= "file"name= "file" />    <inputtype= "submit"name= "submit"value= "upload" /></form></HTML>

PHP files:

<?PHP$file=$_files[' file '];//get the transferred data//get the file name$name=$file[' name '];$type=Strtolower(substr($name,Strrpos($name,‘.‘) +1));//get file types and convert to lowercase$allow _type=Array(' jpg ', ' jpeg ', ' gif ', ' png ');//define which types are allowed to Upload//determine if file types are allowed to uploadif(!In_array($type,$allow _type)){    //Stop the program directly if it is not allowed to run    return ;}//to determine if it was uploaded via HTTP postif(!Is_uploaded_file($file[' Tmp_name '])){    //if it is not uploaded via HTTP post    return ;}$upload _path= "./img/";//upload the file Path//start moving files to the appropriate folderif(Move_uploaded_file($file[' Tmp_name '],$upload _path.$file[' name '])){    Echo"successfully!";}Else{    Echo"failed!";}?>

Simple case of uploading with thinkphp upload class:

    //Uploading configuration information    protected $upconfig=Array(        ' MaxSize ' = 3145728,//3M' Exts ' =Array(' jpg ', ' gif ', ' png ', ' jpeg '), ' rootpath ' = './public/uploads/info/', ' Savepath ' and ', ',//= = Upload subdirectory requires each function to set itself' Savename ' =Array(' uniqid ', '), ' autosub ' =false,//Close subdirectory Save' SubName ' =Array(' date ', ' Ymd '),    ); protected functionUpload$file) {        $res[' result '] = 1; $res[' Imgurl '] = '; $res[' msg '] = ';  do {            $ret=true;
Determine if a path exists$fullPath=$this->upconfig[' RootPath '.$this->upconfig[' Savepath ']; if(!file_exists($fullPath)){
If it does not exist, create a folder$ret=mkdir($fullPath, 0777,true); } if(!$ret) { //Upload error message $res[' result '] = 0; $res[' msg '] = "failed to create path to save picture!" "; break; } //instantiating an upload class $upload=New\think\upload ($this-upconfig); //Upload a single file $info=$upload->uploadone ($file); if(!$info) { //Upload error message $res[' result '] = 0; $res[' msg '] =$upload-GetError (); } Else { //upload successfully get upload file information $imgurl=$this->upconfig[' RootPath '.$info[' Savepath '].$info[' Savename ']; $imgurl=Str_replace(‘./‘, ‘/‘,$imgurl); $res[' result '] = 1; $res[' Imgurl '] =$imgurl; } } while(0); return $res; } //Save the uploaded file $res=$this->upload ($_files[' Attorney ']);

Upload images via the driver app, interface:

Note: iOS or Android: by base64 the image to get the string, passed to the interface

Interface: the received string is Base64 decoded and then uploaded to the specified location via the file_put_contents Function.

    /** * Image upload * @param $imginfo-image of the resource, array type. [' picture type ', ' picture size ', ' picture for base64 encrypted string '] * @param $companyid-company ID * @return Mixed*/     public functionUploadimage ($imginfo,$companyid ) {        $image _type=Strip_tags($imginfo[0]);//type of picture        $image _size=intval($imginfo[1]);//Picture Size        $image _base64_content=Strip_tags($imginfo[2]);//the picture is Base64 encoded string        $upload=NewUploaderservice (); $upconfig=$upload-upconfig; if(($image _size>$upconfig[' maxSize ']) || ($image _size= = 0)) {            $array[' Status '] = 13; $array[' comment '] = "picture size does not meet the requirements!" "; return $array; }        if(!In_array($image _type,$upconfig[' exts '])) {            $array[' Status '] = 14; $array[' comment '] = "picture format does not meet the requirements!" "; return $array; }        //set attachments to upload subdirectories        $savePath= ' bus/group/'.$companyid. ‘/‘; $upload->upconfig[' Savepath '] =$savePath; //name of picture save        $new _imgname=uniqid().Mt_rand(100,999). '. '.$image _type; //base64 decoded picture string        $string _image_content=Base64_decode($image _base64_content); //Save the uploaded file        $array=$upload->upload ($string _image_content,$new _imgname); return $array; }
    //Uploading configuration information     public $upconfig=Array(        ' MaxSize ' = 3145728,//3145728B (bytes) = 3M' Exts ' =Array(' jpg ', ' gif ', ' png ', ' jpeg '),//' rootpath ' = './public/uploads/info/',' RootPath ' = ' https://www.eyuebus.com/Public/Uploads/info/',    ); /** * @param $string _image_content-the string resource of the image to be uploaded * @param $new _imgname-the name of the picture, such as: 57c14e197e2d1744.jpg * @re Turn mixed*/     public functionUpload$string _image_content,$new _imgname) {        $res[' result '] = 1; $res[' Imgurl '] = '; $res[' Comment '] = ';  do {            $ret=true; $fullPath=$this->upconfig[' RootPath '.$this->upconfig[' Savepath ']; if(!file_exists($fullPath)){                $ret=mkdir($fullPath, 0777,true); }            if(!$ret) {                //Upload error message                $res[' result '] = 12; $res[' comment '] = "failed to create path to save picture!" "; return $res;  break; }            //Start uploading            if(file_put_contents($fullPath.$new _imgname,$string _image_content)){                //upload successfully get upload file information                $res[' result '] = 0; $res[' comment '] = "upload succeeded!" "; $res[' imgname '] =$new _imgname; }Else {                //Upload error message                $res[' result '] = 11; $res[' comment '] = "upload failed! "; }        }  while(0); return $res; }

PHP image Upload

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.