Php 为什么图像不保存在laravel上的thumb文件夹中?
我使用的是laravel 5.6 我上传图像的方法如下:Php 为什么图像不保存在laravel上的thumb文件夹中?,php,laravel,image-uploading,laravel-5.6,Php,Laravel,Image Uploading,Laravel 5.6,我使用的是laravel 5.6 我上传图像的方法如下: public function uploadImage($file) { if($file) { $fileName = str_random(40) . '.' . $file->guessClientExtension(); } $destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPAR
public function uploadImage($file) {
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
return $fileName;
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$file->move($destinationPathThumb, $fileName);
代码是有效的。如果该方法运行,它将在产品文件夹中保存一个文件
但我想补充一个逻辑。如果图像成功保存在产品文件夹中,那么它将在产品文件夹中创建一个文件夹缩略图
我尝试在$file->move($destinationPath,$fileName)下面添加此代码代码>如下所示:
public function uploadImage($file) {
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
return $fileName;
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$file->move($destinationPathThumb, $fileName);
然后我运行我的方法,将图像成功上传到产品文件夹中。但图像未成功上载到thumbs文件夹中。代码如下:$file->move($destinationPathThumb,$fileName)代码>不工作
也许我的逻辑代码还是错的
我怎样才能解决这个问题 您需要复制该文件,因为单个图像的多次移动将无法满足您的需要
你可以这样做
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
Storage::copy($destinationPath. DIRECTORY_SEPARATOR . $fileName , $destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
希望您能理解。我认为您需要复制图像,因为通过请求发送图像时,图像只能移动到单个位置。存在如下错误:路径中未找到文件:home/vagrant/code/myapp/storage/app/public/product
@SuccessMan我已更新我的答案,请尝试此错误。旧版本只有目录的路径,但您需要文件的完整路径。谢谢。看看这个:。也许你能帮我好的,我也会看到的。