将干预图像保存在 Laravel 5 的所有者文件夹中
Saving Intervention Image In Owners Folder in Laravel 5
我可以更改我的代码以将上传的图片保存在 public 目录中,但当我想将他们上传的图片保存在一个文件夹中作为他们公司的名称时,则不能。例如什么有效:
/public/company_img/<filename>.jpg
如果用户的公司名称是 Foo,当他们保存上传图片时我想要这个:
/public/company_img/foo/<filename>.jpg
这是在我的控制器中:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
只要看看它,您就可以轻松地了解它在做什么。在我点击更新按钮后,我的日志什么也没显示,浏览器变成空白。任何想法
File::exists($path) or File::makeDirectory($path);
这一行没有意义,因为您检查文件是否存在,如果不存在,您想要尝试创建一个文件夹(在您的 $path 变量中,您保存的是文件路径而不是目录)
我会这样做:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
但我真的不能说你的行为与此有关...没有错误信息,我们只能猜测。
我可以更改我的代码以将上传的图片保存在 public 目录中,但当我想将他们上传的图片保存在一个文件夹中作为他们公司的名称时,则不能。例如什么有效:
/public/company_img/<filename>.jpg
如果用户的公司名称是 Foo,当他们保存上传图片时我想要这个:
/public/company_img/foo/<filename>.jpg
这是在我的控制器中:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
只要看看它,您就可以轻松地了解它在做什么。在我点击更新按钮后,我的日志什么也没显示,浏览器变成空白。任何想法
File::exists($path) or File::makeDirectory($path);
这一行没有意义,因为您检查文件是否存在,如果不存在,您想要尝试创建一个文件夹(在您的 $path 变量中,您保存的是文件路径而不是目录)
我会这样做:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
但我真的不能说你的行为与此有关...没有错误信息,我们只能猜测。