dongqiao1964 2016-01-26 08:15
浏览 194
已采纳

Laravel 5动态地将图像移动到另一个文件夹

I have a fileupload module in my app. I can upload files in img/upload/{container_id}/file_name_here.

The {container_id} will depend on what folder the user will use.

The problem I encountered is when they try to edit a record to another folder. The file that they uploaded remains in the old folder.

I want to move the file also to the new folder the user defines.

I have here my code, I'm stuck in moving the file.

$attachments = Attachment::where('document_id',$id)->select('filename')->get();
        $document = Document::findOrFail($id);

        foreach($attachments as $attachment)
        {
            $attachment->filename = base_path().'/public/img/upload/'.$document->container_id."/".$attachment->filename;
        }

        Document::findOrFail($id)->update($request->all());

        $document = Document::findOrFail($id);

        $x = Attachment::where('document_id','=',$id)->count();

        foreach($attachments as $file)
        {

            HOW_DO_I_MOVE_THE_FILE????
            $x++;
        }

        return redirect('documents');
  • 写回答

1条回答 默认 最新

  • dongyashun2559 2016-01-26 08:54
    关注

    Update:

    In your case, you should use rename():

    rename ('current/path/to/foo', 'new/path/to/foo');
    

    With rename() you can not only rename, but also move! Simply, if the path of the second param differs. So you can use in your loop the attachment path as first param, and a destination path as second.

    Documentation: http://php.net/rename

    Below the Laravel approach for moving after upload.

    From the documentation:

    $request->file('photo')->move($destinationPath);
    
    $request->file('photo')->move($destinationPath, $fileName);
    

    photo is the name of your file upload input element.

    Note, you can use the array notation, if you've multi upload, such as:

    foreach ($request->file('photo') as $photo)
    {
        $photo->move($destinationPath, $chooseYourFileName);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题