简体   繁体   中英

laravel 5 won't delete files from storage section

when I try to delete specific file trough my controller I get an error

Sorry, the page you are looking for could not be found. NotFoundHttpException

This is my controller:

public function destroyFile($file_name)
{
    $file = storage_path('documents').'/'.$file_name;
    Storage::delete($file);
    return redirect('/documents');
}

My route:

  Route::delete('documents/{file}','FilesController@destroyFile');

And my view:

    {!! Form::open(['method' => 'DELETE', 'action' =>     ['FilesController@destroyFile', $file->name] ]) !!}
                            {!! Form::hidden('_method', 'DELETE') !!}
                            {!! Form::token() !!}
                            {!! Form::submit(trans('buttons.del-cat'),['class'=>'btn btn-danger user-delete push-right']) !!}
                            {!! Form::close() !!}

I had the same problem, everything was set right but it still didn't work.

Instead of Storage::delete() I used File::delete() , that fixed it.

Hope this works!

public function images(Request $request,$id){
    $user_id=$request->session()->get('user_id');
    $data=DB::table('company_images')->where('id',$id)->first();
    //deleteing the image from database and server
    DB::table('company_images')->where('id', '=', $id)->delete();
    //delete the image form the server
    $path=public_path("/assets/img/company/$data->image");
    unlink($path);
    return redirect("/company/$user_id/add_details")->with('delete','delete');

}

this is function i used to delete company images using unlink($path) this works well with me also the big note:you should write carefully the path to file you delete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM