0

I want to delete files from server which have been uploaded through Dropzone.But,Only thumbnails have been deleted.File in the server not erased.I have got an error in console.http://localhost:8000/upload/delete 500 (Internal Server Error)'

My Upload Method In Controller

public function upload(Request $request){
           $file= $request->file('file');
           $filename=$file->getClientOriginalName();
           $upload='uploads/topics';
           $file->move($upload, $filename);
    }

Dropzone Script file.

Dropzone.options.addImages = {
maxFilesize: 8,
addRemoveLinks: true,
dictRemoveFile: 'Remove',

init:function() {

        this.on("removedfile", function(file) {

            $.ajax({
                type: 'POST',
                url: 'upload/delete',
                data: {id: file.name},
                dataType: 'html',
                 success: function(data){
                    var rep = JSON.parse(data);

                 }
            });

        } );
    },
}

My delete method in controller.

public function delete(Request $request){
         $filename = $request->input('id');
          unlink('uploads/topics'.$filename);

    }
Asm Arman
  • 359
  • 6
  • 24

1 Answers1

1

Two issues that I can see right away:

  1. In your delete controller method you are trying to access $request but you haven't injected it.

  2. The request input method is lowercase.

I believe this is closer to what you need:

public function delete(Request $request){    
    $filename = $request->input('id');
    unlink('uploads/topics/' . $filename);
}

Some notes:

  1. Whenever you get an "internal server error" that means you need to check your error logs. There are details in one of your log files that will tell you the exact error.

  2. Right now your delete method could allow a user to delete things you may not want them to delete. I could easily post a filename to that endpoint and delete anything from your topics folder.

  3. Even more dangerous, this code appears to be at risk for a traversal attack. See here for details: https://www.owasp.org/index.php/Path_Traversal

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • I have updated the cod as per you say.But This is not working,How can i check log files? – Asm Arman Jun 12 '16 at 20:11
  • You need to be more specific than just "not working." Your error logs depend on what webserver you're using. Maybe this will help you get started: http://stackoverflow.com/a/5127884/660694 – jszobody Jun 12 '16 at 20:14
  • You should also do some basic troubleshooting in your `delete` method. Do a `dd($request->all())` to make sure you're getting the id parameter. Do a `dd('uploads/topics/' . $filename);` before the unlink, to make sure the path looks good. This is just debugging 101 stuff here. – jszobody Jun 12 '16 at 20:25
  • Look also in the `app/Http/routes.php` file. Is your delete route really `/upload/delete` or is it possible just `/delete`? – jszobody Jun 12 '16 at 20:32
  • i do a dd($request->all()) as you say.I also do a dd('uploads/topics/' . $filename);But Nothing happened.I am getting the same error.I am using wamp server. – Asm Arman Jun 13 '16 at 06:52
  • That means your issue is somewhere else, in code that you haven't posted. Did you look at the routes file as I suggested? Also, you need to research where WAMP is putting your error log files. Do some searching on that, you absolutely need to know where the error logs are for the future. – jszobody Jun 13 '16 at 21:19