file does not exist 阿里雲OSS圖片上傳遇到的問題


./uploads\20171209/0497b8dd16e72c6fcf5bfd552f535a81.png  file does not exist

原代碼

function aliyun($savePath,$category='',$isunlink=false,$bucket="dddgame"){
        $accessKeyId = config('aliyun_oss.accessKeyId');//去阿里雲后台獲取秘鑰
        $accessKeySecret = config('aliyun_oss.accessKeySecret');//去阿里雲后台獲取秘鑰
        $endpoint = config('aliyun_oss.endpoint');//你的阿里雲OSS地址
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        // 判斷bucketname是否存在,不存在就去創建
        if( !$ossClient->doesBucketExist($bucket)){
            $ossClient->createBucket($bucket);
        }
        $category=empty($category)?$bucket:$category;

        $savePath = str_replace("\\","/",$savePath);

        $object = $category.'/'.$savePath;//想要保存文件的名稱
        $file =  './uploads\\'.$savePath;//文件路徑,必須是本地的。

        try{
            $ossClient->uploadFile($bucket,$object,$file);
            if ($isunlink==true){
                unlink($file);
            }
        }catch (OssException $e){
            $e->getErrorMessage();
        }
        $oss=config('aliyun_oss.url');
        return $oss."/".$object;
    }

這段代碼在windows下正常,在linux里就有問題了。

錯就錯在 $file = './uploads\\'.$savePath;//文件路徑,必須是本地的。,linux 的目錄結構跟windows不一致。

修正后,

function aliyun($savePath,$category='',$isunlink=false,$bucket="dddgame"){
        $accessKeyId = config('aliyun_oss.accessKeyId');//去阿里雲后台獲取秘鑰
        $accessKeySecret = config('aliyun_oss.accessKeySecret');//去阿里雲后台獲取秘鑰
        $endpoint = config('aliyun_oss.endpoint');//你的阿里雲OSS地址
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        //        判斷bucketname是否存在,不存在就去創建
        if( !$ossClient->doesBucketExist($bucket)){
            $ossClient->createBucket($bucket);
        }
        $category=empty($category)?$bucket:$category;

        $savePath = str_replace("\\","/",$savePath);

        $object = $category.'/'.$savePath;//想要保存文件的名稱
        $file =  './uploads/'.$savePath;//文件路徑,必須是本地的。

        try{
            $ossClient->uploadFile($bucket,$object,$file);
            if ($isunlink==true){
                unlink($file);
            }
        }catch (OssException $e){
            $e->getErrorMessage();
        }
        $oss=config('aliyun_oss.url');
        return $oss."/".$object;
}

正常了!!!!

厲害了,胡總。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM