代码之家  ›  专栏  ›  技术社区  ›  Carlos Salazar

如何从laravel 5.6中的路径创建文件实例

  •  0
  • Carlos Salazar  · 技术社区  · 6 年前

    我想知道是否有方法创建 file 就像用户发送 input file

    我有一个文件夹 backups 然后用户可以选择要恢复的文件,我希望将它们从备份文件夹移动到用户要恢复的模型的文件夹中

    $path = public_path('images/news/');
        foreach($news as $new) {
            foreach($new->images as $image) {
                $imagepath = public_path('images/backup/news/'.$image->image);
                if(file_exists($imagepath)) {
    
                    $this->fileCtrl->copy($imagepath, $path.$new->id);
                }
            }
        }
    

    $this->fileCtrl 是一个使用实例 Illuminate\Filesystem\Filesystem as File;

    这将尝试移动文件,但不是创建一个包含id和文件的文件夹,而是创建一个包含id且不包含扩展名的文件。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Shobi    6 年前

    显然需要创建目录

    if (!file_exists($imagepath)) {
        //if path doesn't exist create the path and copy
        mkdir($imagepath, 0777, true);
        $this->fileCtrl->copy($imagepath, $path.$new->id);
    }else{
        //just copy file to path
        $this->fileCtrl->copy($imagepath, $path.$new->id);
    }