在Laravel 5中保存干预图像在所有者文件夹中

时间:2015-04-09 09:10:21

标签: laravel laravel-5 intervention

我可以更改我的代码以将上传的图像保存在公共目录中,但是当我想将他们上传的图像保存在文件夹中作为他们公司的名称时。例如:什么有效:

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

只要看看你可以很容易地看到它在做什么。点击更新按钮后,我的日志没有显示任何内容,浏览器变为空白。任何想法

1 个答案:

答案 0 :(得分:4)

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

但我真的不能说你的行为与此有关......没有错误信息,我们只能猜测。