Php 如何在laravel上用一种方法同时上传两个文件夹中的文件?
我使用的是laravel 5.6 我想一次上传两个文件夹中的文件。因此,如果图像成功上传到产品文件夹中,我希望图像也上传到thumbs文件夹中 我试着这样:Php 如何在laravel上用一种方法同时上传两个文件夹中的文件?,php,laravel,file-upload,image-uploading,laravel-5.6,Php,Laravel,File Upload,Image Uploading,Laravel 5.6,我使用的是laravel 5.6 我想一次上传两个文件夹中的文件。因此,如果图像成功上传到产品文件夹中,我希望图像也上传到thumbs文件夹中 我试着这样: public function uploadImage($file) { if($file) { $fileName = str_random(40) . '.' . $file->guessClientExtension(); } $destinationPath = storage_
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);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
$file = $file->move($destinationPath, $fileName);
如果代码运行,则只需将其成功上载到产品文件夹中即可。它没有上传到thumbs文件夹中
存在如下错误:
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);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
$file = $file->move($destinationPath, $fileName);
消息找不到文件/tmp/phpUSxbEJ
异常干预\Image\exception\NotReadableException
我尝试运行以下代码:
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
所以我删除了要上传到产品文件夹中的代码。我试过了,效果很好。它成功上传到thumbs文件夹
所以我认为在一个过程中,它只上传到一个文件夹中
是否有其他方法可以在2个文件夹上上载 您首先上载临时文件,该文件将在您将其保存到磁盘上后被删除,这就是为什么您不能重复使用它的原因。您将获取保存的图像并对其进行大小调整,然后使用其他名称保存它:
public function uploadImage($file)
{
...
$file->move($destinationPath, $fileName);
//$file here doesn't exist anymore, hence it can't be read
...
$uploadedFile = Storage::get($destinationPath . DIRECTORY_SEPARATOR . $filename);
$image_resize = Image::make($uploadedFile);
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $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);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
$file = $file->move($destinationPath, $fileName);
它在保存$file后工作,然后使用Storage::get检索保存的文件,而不是临时文件$file@NikolaGavric试着用代码回答这个问题。让我来clearer@SuccessMan您的http请求post方法在哪里?@Sagar Gautam在控制器上。我上面的代码放在repositoriesEh好的,这样做很好,你在使用设计模式吗?您找到解决方案了吗?存在类似这样的错误:找不到类“App\Repositories\Storage”如果您花一分钟阅读,您可以看到您必须使用light\Support\Facades\Storage;仍然存在错误。错误:message/home/vagrant/code/my-app/storage/app/public/product/gckx3YQrbc8CnKMaXjlpjy9FnejYpbjKetMHXKGN.jpeg。异常\Contracts\Filesystem\FileNotFoundException