【问题标题】:Laravel 5.7 Can't write image data to path Image interventionLaravel 5.7 无法将图像数据写入路径图像干预
【发布时间】:2019-02-20 17:36:21
【问题描述】:

我想将我上传的所有文件都公开。

超级管理员每次创建新公司时都会创建一个可以在公众中找到的文件夹

$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']);

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

问题:这个错误的原因是什么,我该如何解决?

【问题讨论】:

  • Laravel 是否拥有该目录所需的权限?
  • 每当我创建新公司时,我总是检查新创建的文件夹,允许修改、读取、写入、读取和执行(选中)
  • 我更新了我的帖子,我在创建新公司后举了一些例子。

标签: laravel-5.7 intervention


【解决方案1】:

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

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 2019-02-25
    • 2016-01-15
    • 1970-01-01
    • 2014-10-19
    • 2015-09-23
    • 2021-08-31
    • 2020-03-29
    相关资源
    最近更新 更多