天天看點

php擷取本地檔案上傳

二開雲豹時遇到七牛雲不好使的情況,也不知道是因為配置的問題還是雲豹的七牛雲版本問題,反正就是不行,于是自己另起一個站點作為七牛雲的…因為實在不會這個π架構````

//圖檔轉接上傳七牛雲
  public function getFile(){
    $datas = $this->request->param();
    $file = "檔案路徑";
    $key = "上傳七牛雲的檔案名";

    header('Content-type:text/html; charset=utf-8'); //聲明編碼
    $ch = curl_init();
    
    $files = "檔案的路徑(不管絕對還是相對,能找到就行)";
    $url = 'http://xxx.com/xxx/xxx/xxx?key='.$key."&files=".$files;//下面那個方法的路徑,傳參請根據業務需要來

    $rr = curl_file_create($files);
    //post資料,使用@符号,curl就會認為是有檔案上傳
    $curlPost = array('image'=>$rr);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1); //POST送出
    curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
    $data =curl_exec($ch);
    curl_close($ch);
  }
           
/**
   * 圖檔上傳     上傳到七牛雲
   * @return String 圖檔的完整URL
   */
  public function qiniuUpload()
  {
	//七牛雲怎麼用就不贅述了,這就是正常的七牛雲上傳,主要地方在上一個方法

    $data = $this->request->param();
    $file = request()->file('image');
    $key = $data['key'];
    $files = $data['files'];

    // 要上傳圖檔的本地路徑
    $filePath = $file->getRealPath();
    $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //字尾
    //擷取目前控制器名稱
    // $controllerName=$this->getContro();
    // 上傳到七牛後儲存的檔案名
    require_once APP_PATH . '/../vendor/qiniu/autoload.php';
    // 需要填寫你的 Access Key 和 Secret Key
    $accessKey = config('ACCESSKEY');
    $secretKey = config('SECRETKEY');
    // 建構鑒權對象
    $auth = new Auth($accessKey, $secretKey);
    // 要上傳的空間
    $bucket = config('BUCKET');
    $domain = config('DOMAINImage');
    $token = $auth->uploadToken($bucket);
    // 初始化 UploadManager 對象并進行檔案的上傳
    $uploadMgr = new UploadManager();
    // 調用 UploadManager 的 putFile 方法進行檔案的上傳
    list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

    if ($err !== null) {
      // return $this->info('10004','上傳失敗');
    } else {
    	//删除本地的檔案
      if(file_exists($files)) {
        unlink($files);
      }
      // return $this->info('10000','上傳成功',$data);
    }
    
  }
           

留作記錄