Laravel 5.2 Storage::makeDirectory($dir) 未创建目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38104912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:04:37  来源:igfitidea点击:

Laravel 5.2 Storage::makeDirectory($dir) is not creating directory

phplaravelubuntupermissionslaravel-5.2

提问by Iliyan

I am getting confused with this logic. I am using Laravel 5.2 Storage::makeDirectory to create two paths, first (video/) is created correctly and the other (thumbnails/) don't.

我对这个逻辑感到困惑。我正在使用 Laravel 5.2 Storage::makeDirectory 创建两个路径,第一个(video/)被正确创建,另一个(缩略图/)没有。

$user = 1;

if(!File::exists(public_path() . "/video/$user"))
{
    Storage::makeDirectory(public_path() . "/video/$user", 0777);
}

$file = rand(1111111111111, 9999999999999);
$imgpath = public_path() . "/thumbnails/$file";

if(!File::exists($imgpath))
{
    Storage::makeDirectory($imgpath, 0777);
}

Here is the permission configuration:

下面是权限配置:

drwxrwxrwx 2 ubuntu ubuntu 4096 Jun 28 19:33 thumbnails/
drwxrwxrwx 3 ubuntu ubuntu 4096 Jun 29 15:21 video/

I also could create a directory from cli with the given string from $imgpath:

我还可以使用$imgpath 中的给定字符串从 cli 创建一个目录:

mkdir /home/ubuntu/workspace/site/public/thumbnails/6300643852187

Any suggestions?

有什么建议?

回答by

Well... I figured out what was going on.

嗯……我想通了是怎么回事。

File::exists(public_path() . "/video/$user")is looking for this:

File::exists(public_path() . "/video/$user")正在寻找这个:

/home/ubuntu/workspace/site/public/video/N

/home/ubuntu/workspace/site/public/video/ N


And Storage::makeDirectory(public_path() . "/video/$user")is creating a directory in:


并且Storage::makeDirectory(public_path() . "/video/$user")正在创建一个目录:

/home/ubuntu/workspace/site/storage/app/public/video/N

/home/ubuntu/workspace/site/storage/app/public/video/ N


So I can go to site/config/filesystems.phpand change the routes for appand app/public; you can check them by using the helper storage_path('app').
But, instead, I decided to save the videos in the storage/app/public/videoand storage/app/public/thumbnails, and I am getting some package's error.


所以我可以去site/config/filesystems.php并更改appapp/public的路由;您可以使用 helper 来检查它们storage_path('app')
但是,相反,我决定将视频保存在storage/app/public/videostorage/app/public/thumbnails 中,并且我收到了一些包的错误。

But, both paths are writable and should be working by now.

但是,这两条路径都是可写的,现在应该可以工作了。

回答by Sadee

Laravel 5.2

Laravel 5.2

if(!Storage::disk('public')->has('image/path/directory/')){
    Storage::disk('public')->makeDirectory('image/path/directory/');
}

回答by Iliyan

You need to force it to do it like this

你需要强迫它这样做

$dr = $_SERVER['DOCUMENT_ROOT'];
$is_dir = File::makeDirectory($dr.'/uploads/images/'.$user.'/thumbs/', 0755, true, true);

0755 is folder permissions

0755 是文件夹权限

1st true is recursive creation of folders

第一个 true 是递归创建文件夹

2nd true is to force it to do it

第二个是强迫它去做