Laravel 5.7无法将图像数据写入路径图像干预

问题描述 投票:0回答:1

我想将所有上传的文件保存到公开内容中。

每当superadmin创建一个新公司时,它将创建一个可以在公共场所找到的文件夹

$path = public_path().'/attachments/'.$company_id;
if (!File::exists($path)) {
                File::makeDirectory($path, 777, true, true);
}

当我试图上传公司徽标时,它给了我一个错误"Can't write image data to path (C:\wamp64\www\accubooksv2\public/attachments/10/C-CP//1550648014.jpg)"

调节器

$data['company_id'] = Auth::user()->company_id;
        $image = $request->file('image');
        $data['imagename'] = time().'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path().'/attachments/'.$data['company_id'].'/C-CP/';
        // $path = public_path().'/attachments/'.$data['company_id'].'/C-CP/';
        // File::makeDirectory(public_path()."uploads/properties");
        // File::makeDirectory($path,0777,true);
        $img = Image::make($image);
         $img->resize(100, 100, function ($constraint) {
            $constraint->aspectRatio();
        })->save($destinationPath.'/'.$data['imagename']);

这是我创建新公司后创建的新文件夹的示例。 enter image description here

问题:此错误的原因是什么?如何解决此问题?

laravel-5.7 intervention
1个回答
0
投票

错误的线索可能在文件路径中:

C:\wamp64\www\accubooksv2\public/attachments/10/C-CP//1550648014.jpg)

混合目录分隔符可能导致错误。如果将目录分隔符从/更改为\会发生什么。

如果您想使代码跨平台,请使用DIRECTORY_SEPARATOR而不是\/。例如。:

$path = public_path() . DIRECTORY_SEPARATOR . 'attachments'. DIRECTORY_SEPARATOR . $company_id;

要么

$path = join(DIRECTORY_SEPARATOR, array('attachments', $company_id);
$path = public_path($path);
© www.soinside.com 2019 - 2024. All rights reserved.