【问题标题】:Laravel: create an upload directory on the flyLaravel:动态创建上传目录
【发布时间】:2014-08-09 18:29:39
【问题描述】:

我有这个,

$destinationPath = public_path(). '/img/'. $username;

我以为可以创建目录,但它想出了一个

Symfony \ Component \ HttpFoundation \ File \ Exception \ FileException

Unable to create the "/img/alvarito" directory

知道有什么问题吗?在我看来,它还试图创建已经存在的 /img/,我自然想要的是在“img”目录中为每个用户在上传文件时创建子目录。

非常感谢

一个

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你可以试试这个:

    $destinationPath = public_path(). '/img/'. $username;
    
    if(!file_exists($destinationPath)) File::makeDirectory($destinationPath);
    

    如果目录不存在,这将创建目录。

    【讨论】:

    • 你好。感谢您的回答,我收到了这样的回复:mkdir(): No such file or directory
    • 可能是权限问题吗?我使用的是专用服务器,如何检查权限?
    • 可能是权限问题,Check this answer.
    • 我直接浏览过Filezilla,目录img有0755,是不是说,7=可以读任何东西,5可以读执行不写?是这个原因吗?但那是一个公共目录,我不希望未注册的人写在目录中。但是,有人告诉我,图像保存在公共目录中。
    • 是的,7 = 4+2+1 (read/write/execute) 6 = 4+2 (read/write) 5 = 4+1 (read/execute) 4 = 4 (read) 3 = 2+1 (write/execute) 2 = 2 (write) 1 = 1 (execute)
    猜你喜欢
    • 2015-07-21
    • 2020-08-12
    • 1970-01-01
    • 2016-12-21
    • 2011-11-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多