我想将所有上传的文件保存为公开文件。
每次超级管理员创建新公司时,都会创建一个可以在公共场所找到的文件夹
$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']);
问题:此错误的原因是什么,我该如何解决?
答案 0 :(得分: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);