【问题标题】:laravel 8 api : delete file from folder when updating postlaravel 8 api:更新帖子时从文件夹中删除文件
【发布时间】:2021-08-20 15:09:07
【问题描述】:

我有帖子表并为它上传图片,所以在PostController()update() 方法中我想编辑帖子和图片

为了编辑图像,我想从文件夹和数据库中删除最后的图像,并替换新图像。

所以我可以从数据库中删除最后的图像,但我不能从文件夹中删除它们

控制器:

public function update(StorePostRequest $request , $id )
    {

        $validatedData = $request->all();

        if ($request->hasfile('image')) {

            $postTitle = $request->title; //post title for folder name and the images inside it

            //delete last Images from database for updating images
            Image::where('imageable_type' , 'App\Models\Post')->where('imageable_id' , $id)->delete();

             $directory =storage_path().'/images/posts/'.$postTitle;
            if($directory){
                Storage::disk('public')->deleteDirectory('images/posts/.$postTitle');
            }


            $files = $request->file('image');

            foreach ($files as $file) {

                $imageName = time().rand(1,10000).'.'.$file->extension();

                $postTitle = $request->title; //post title for folder name and the images inside it
                $imagePath = public_path(). '/images/posts/'.$postTitle;

                $file->move($imagePath, $imageName);

                $image = new Image;
                $image->image = $imageName;
                $image->path = $imagePath;

                $images[] = $image; // make an array of uploaded images
            }
        }

        $post = Post::find($id);

        $post->category_id = $validatedData['category_id'];
        $post->title = $validatedData['title'];
        $post->body = $validatedData['body'];
        $post->study_time = $validatedData['study_time'];

        $post->save();
        $post->images()->saveMany($images);

        return response()->json([
        "success" => true,
        "message" => "successfully",
        "data" => $post
        ]);
    }


帖子和图片之间的关系是:一对多的多态,我用邮递员来品尝它

我试过这种方法,但它不起作用:

             File::delete(public_path('/images/posts/'.$postTitle));

             Storage::disk('public')->deleteDirectory($directory);

另外,当我尝试时

       Storage::disk('public')->deleteDirectory('images/posts/');

它可以工作并且帖子文件夹已被删除,但问题来自$postTitle

当我使用它时,它不起作用

【问题讨论】:

  • 您要从图像文件夹中删除图像吗? ?问题不清楚
  • 是的,我编辑我的问题..请再检查一遍

标签: php laravel api file-upload


【解决方案1】:

您可以使用此代码删除文件

if (!is_dir(public_path('/images/posts/'.$postTitle))) {
    mkdir(public_path('/images/posts/'.$postTitle));
}

【讨论】:

  • 你的图片的完整路径是什么
  • 在公共文件夹中,我有图像文件夹:public/images/posts/postTitle。 postTitle 是文件夹名称,帖子的图像在此文件夹中
  • 试试这个新代码if (!is_dir(public_path('/images/posts/'.$postTitle))) { mkdir(public_path('/images/posts/'.$postTitle)); }
【解决方案2】:

如果您需要删除目录,请使用Storage::disk('public')->deleteDirectory('images/posts');

【讨论】:

  • 感谢它的工作,但仅适用于帖子文件夹,我想删除帖子目录中的 postTitle 目录,(删除最后的帖子图片而不是所有帖子图片)
  • @dumpAndDie 所以只要像你 Storage::disk('public')->deleteDirectory('images/posts/' . $postTitle); 那样在路径的末尾添加一个目录名或者我没明白?
  • 它不起作用(无法删除目录)
  • @dumpAndDie idk,刚刚测试过,一切正常。有什么错误吗?权限?可能是您在 apache 下运行代码并且文件夹所有者错误?
  • 不,没有问题,因为可以删除帖子文件夹..我认为我的方法是错误的,为了更新图像,我必须尝试另一种方法来删除最后的图像并替换新图像
猜你喜欢
  • 2021-10-29
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多