Laravel 5.4 替换文件(如果存在)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44709596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:11:44  来源:igfitidea点击:

Laravel 5.4 replace file if exist

phplaravellaravel-5laravel-5.3laravel-5.4

提问by Edward Palen

I need to solve a small inconvenience that I am presenting with the update of an image when modifying the associated registry.

我需要解决在修改关联注册表时更新图像时出现的一个小不便。

When I modify the registry I want to replace the associated file that is in the directory.

当我修改注册表时,我想替换目录中的关联文件。

This is my table structure:

这是我的表结构:

Schema::create('institutions', function(Blueprint $table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('initials')->nullable();
            $table->string('description')->nullable();
            $table->string('avatar')->default('default.jpg');
            $table->timestamps();
        });

This is my update method on my controller:

这是我的控制器上的更新方法:

public function update(Request $request, $id)
    {
        //

        $institution = $this->institution->find($id);

        try
        {
            $institution->update($request->all());

            if($request->hasFile('avatar')){
                $avatar = $request->file('avatar');
                $filename = time() . '.' . $avatar->getClientOriginalExtension();
                Image::make($avatar)->resize(250, 205)->save( public_path('uploads/institutions/' . $filename ) );

                $institution->avatar = $filename;
                $institution->save();
            }

            $updated = $institution;

            $message = flash('Institución actualizada correctamente!!!', 'success');

            return redirect()->route('instituciones.index')->with('message', $message);    
        }       

        catch(\Illuminate\Database\QueryException $e)
        { 
            $message = flash('La institución no se actualizó correctamente!!!', 'danger');

            return redirect()->route('institutions.create')->with('message', $message); 
        }   

    }

I have tried different methods but I have not succeeded.

我尝试了不同的方法,但没有成功。

回答by Sovary

The image uploaded must have the same filename to replace the old one,but in this case it would not replace because of time()method.

上传的图片必须具有相同的文件名才能替换旧的,但在这种情况下,由于time()方法的原因,它不会替换。

You may delete old one by get file name from database and save new image

您可以通过从数据库中获取文件名来删除旧图像并保存新图像

//find data by id
$institution = $this->institution->find($id);
$filename  = public_path('uploads/institutions/').$institution->avatar;
if(File::exists($filename)) {

  $avatar = $request->file('avatar');
  $filename_new = time() . '.' . $avatar->getClientOriginalExtension();
  Image::make($avatar)->resize(250, 205)->save( public_path('uploads/institutions/' . $filename_new ) );

  //update filename to database
  $institution->avatar = $filename_new;
  $institution->save();    
  //Found existing file then delete
  File::delete($filename);  // or unlink($filename);
}

Hope it would help

希望它会有所帮助