PHP 圖片上傳

來源:互聯網
上載者:User

標籤:

 

PHP上傳的簡單案例:  

Html檔案:

<html><form action="index.php" name="form" method="post" enctype="multipart/form-data">    <input type="file" name="file" />    <input type="submit" name="submit" value="上傳" /></form></html>

PHP檔案:

<?php$file = $_FILES[‘file‘];//得到傳輸的資料//得到檔案名稱$name = $file[‘name‘];$type = strtolower(substr($name,strrpos($name,‘.‘)+1)); //得到檔案類型,並且都轉化成小寫$allow_type = array(‘jpg‘,‘jpeg‘,‘gif‘,‘png‘); //定義允許上傳的類型//判斷檔案類型是否被允許上傳if(!in_array($type, $allow_type)){    //如果不被允許,則直接停止程式運行    return ;}//判斷是否是通過HTTP POST上傳的if(!is_uploaded_file($file[‘tmp_name‘])){    //如果不是通過HTTP POST上傳的    return ;}$upload_path = "./img/"; //上傳檔案的存放路徑//開始移動檔案到相應的檔案夾if(move_uploaded_file($file[‘tmp_name‘],$upload_path.$file[‘name‘])){    echo "Successfully!";}else{    echo "Failed!";}?>

 

使用thinkphp上傳類上傳的簡單案例:

    // 上傳配置資訊    protected $upconfig = array(        ‘maxSize‘    =>    3145728,         // 3M        ‘exts‘       =>    array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘),        ‘rootPath‘   =>    ‘./Public/Uploads/info/‘,        ‘savePath‘   =>    ‘‘,                          // => 上傳子目錄需要每個函數自己去設定        ‘saveName‘   =>    array(‘uniqid‘,‘‘),        ‘autoSub‘    =>    false,                       // 關閉子目錄儲存        ‘subName‘    =>    array(‘date‘,‘Ymd‘),    );    protected function upload($file) {        $res[‘result‘] = 1;        $res[‘imgurl‘] = ‘‘;        $res[‘msg‘] = ‘‘;        do {            $ret = true;
       //判斷路徑是否存在 $fullPath = $this->upconfig[‘rootPath‘] . $this->upconfig[‘savePath‘]; if(!file_exists($fullPath)){
//如果不存在,建立檔案夾 $ret = mkdir($fullPath, 0777, true); } if(!$ret) { // 上傳錯誤提示錯誤資訊 $res[‘result‘] = 0; $res[‘msg‘] = "建立儲存圖片的路徑失敗!"; break; } // 執行個體化上傳類 $upload = new \Think\Upload($this->upconfig); // 上傳單個檔案 $info = $upload->uploadOne($file); if(!$info) { // 上傳錯誤提示錯誤資訊 $res[‘result‘] = 0; $res[‘msg‘] = $upload->getError(); } else { // 上傳成功 擷取上傳檔案資訊 $imgurl = $this->upconfig[‘rootPath‘] . $info[‘savepath‘].$info[‘savename‘]; $imgurl = str_replace(‘./‘, ‘/‘, $imgurl); $res[‘result‘] = 1; $res[‘imgurl‘] = $imgurl; } } while(0); return $res; } // 儲存上傳的檔案 $res = $this->upload($_FILES[‘attorney‘]);

 

通過司機端App上傳圖片,介面:

註:IOS或者安卓端:通過把圖片進行base64編碼得到字串,傳給介面

      介面端:把接收的字串進行base64解碼,再通過file_put_contents函數,上傳到指定的位置

    /**     * 圖片上傳     * @param $imginfo - 圖片的資源,數群組類型。[‘圖片類型‘,‘圖片大小‘,‘圖片進行base64加密後的字串‘]     * @param $companyid - 公司id     * @return mixed     */    public function uploadImage( $imginfo , $companyid ) {        $image_type = strip_tags($imginfo[0]);  //圖片類型        $image_size = intval($imginfo[1]);  //圖片大小        $image_base64_content = strip_tags($imginfo[2]); //圖片進行base64編碼後的字串        $upload = new UploaderService();        $upconfig = $upload->upconfig;        if(($image_size > $upconfig[‘maxSize‘]) || ($image_size == 0)) {            $array[‘status‘] = 13;            $array[‘comment‘] = "圖片大小不符合要求!";            return $array;        }        if(!in_array($image_type,$upconfig[‘exts‘])) {            $array[‘status‘] = 14;            $array[‘comment‘] = "圖片格式不符合要求!";            return $array;        }        // 設定附件上傳子目錄        $savePath = ‘bus/group/‘ . $companyid . ‘/‘;        $upload->upconfig[‘savePath‘] = $savePath;        //圖片儲存的名稱        $new_imgname = uniqid().mt_rand(100,999).‘.‘.$image_type;        //base64解碼後的圖片字串        $string_image_content = base64_decode($image_base64_content);        // 儲存上傳的檔案        $array = $upload->upload($string_image_content,$new_imgname);        return $array;    }
    // 上傳配置資訊    public $upconfig = array(        ‘maxSize‘    =>    3145728,         //3145728B(位元組) = 3M        ‘exts‘       =>    array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘),//        ‘rootPath‘   =>    ‘./Public/Uploads/info/‘,        ‘rootPath‘   =>    ‘https://www.eyuebus.com/Public/Uploads/info/‘,    );    /**     * @param $string_image_content - 所要上傳圖片的字串資源     * @param $new_imgname - 圖片的名稱,如:57c14e197e2d1744.jpg     * @return mixed     */    public function upload($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) {                // 上傳錯誤提示錯誤資訊                $res[‘result‘] = 12;                $res[‘comment‘] = "建立儲存圖片的路徑失敗!";                return $res;                break;            }            //開始上傳            if (file_put_contents($fullPath.$new_imgname, $string_image_content)){                // 上傳成功 擷取上傳檔案資訊                $res[‘result‘] = 0;                $res[‘comment‘] = "上傳成功!";                $res[‘imgname‘] = $new_imgname;            }else {                // 上傳錯誤提示錯誤資訊                $res[‘result‘] = 11;                $res[‘comment‘] = "上傳失敗!";            }        } while(0);        return $res;    }

 

PHP 圖片上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.