【问题标题】:i want to create directory with the name of the Company and i want to move some files related to that Company in that directory. How can i do it?我想用公司名称创建目录,我想在该目录中移动一些与该公司相关的文件。我该怎么做?
【发布时间】:2023-01-04 00:42:24
【问题描述】:

我在控制器中使用此代码成功创建了目录:

$cpath = public_path().'/files/completed/'.$file['customer_name'];
                if (! File::exists($cpath)) {
                    File::makeDirectory($cpath);
                }

但我不确定如何将相关文件移动到这些目录,因为我试过这个:

File::move('files/pending/'.$filename,'files/completed/'.$file['customer_name'].$filename);

但问题是我没有在各自的目录中获取任何文件,但文件正在移动,名称重命名为 (companyname filename.850) 这不是我想要的,这里是我如何在控制器中为目录和文件:

 $cpath = public_path().'/files/completed/'.$file['customer_name'];
                if (! File::exists($cpath)) {
                    File::makeDirectory($cpath);
                }
       File::move('files/pending/'.$filename,'files/completed/'.$file['customer_name'].$filename);

感谢您的答复!

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    看起来您正在使用 File facade 来创建目录和移动文件。

    要将文件移动到特定目录,您需要将目标目录的完整路径指定为移动方法的第二个参数。现在,您只是将 customer_name 与文件名连接起来,这就是文件被重命名的原因。

    尝试将移动行更新为以下内容:

    File::move('files/pending/'.$filename, $cpath . '/' . $filename);
    

    这会将文件以其原始文件名移动到 $cpath 目录。

    此外,请确保 public_path() 函数返回项目中公共目录的正确路径。您可以尝试打印出 $cpath 的值,看看它是否指向正确的目录。

    我希望这有帮助!

    【讨论】:

      最近更新 更多