2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > file does not exist 阿里云OSS图片上传遇到的问题

file does not exist 阿里云OSS图片上传遇到的问题

时间:2023-06-12 03:44:28

相关推荐

file does not exist 阿里云OSS图片上传遇到的问题

./uploads\1209/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;}

正常了!!!!

厉害了,胡总。

本文转自TBHacker博客园博客,原文链接:/jiqing9006/p/801.html,如需转载请自行联系原作者

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。