【问题标题】:Saving Intervention Image In Owners Folder in Laravel 5在 Laravel 5 的所有者文件夹中保存干预图像
【发布时间】:2015-04-09 20:26:03
【问题描述】:

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

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

只需查看它,您就可以轻松了解它在做什么。我的日志没有显示任何内容,并且在我点击更新按钮后浏览器变为空白。任何想法

【问题讨论】:

  • 执行 PHP 的用户(即网络服务器)是否具有在该位置创建文件和文件夹所需的权限?
  • @Joe 是的。我只得到一个空白浏览器。没有日志,没有错误。
  • File::exists($path) or File::makeDirectory($path); 是有效的 PHP 语法吗?我从未见过以这种格式编写的 IF 语句。
  • 你能发布你完整的控制器功能吗?空白页可能只是因为您的控制器没有回显或返回任何内容
  • @shock_gone_wild 见这里:http://laravel.io/bin/xKY8P

标签: laravel laravel-5 intervention


【解决方案1】:
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

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

【讨论】:

  • 你是我告诉你的老板!我什至得到了正确的 if 语句,但仍然存在写权限问题等!最后一件事,我如何引用用户的图像路径并存储在数据库中?
猜你喜欢
  • 2016-01-11
  • 1970-01-01
  • 2015-05-19
  • 2015-10-31
  • 1970-01-01
  • 2018-01-11
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多