Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE method on jQuery Blueimp FileUpload not allowed using Codeigniter/PHP (405 error)?

I'm trying to delete files using this very nice jQuery Blueimp File Upload plugin.

I put this plugin in my root directory and was able to upload and delete files no problem.

However, when I embed this plugin within my codeigniter app I'm no longer to delete files I've uploaded due to a 405 error. I've set all the folders to 777 just to make sure that isn't an issue.

Any thoughts? Here's my console log:

Console.log

like image 496
tim peterson Avatar asked Aug 08 '12 18:08

tim peterson


2 Answers

I solved my own problem following the code in one of the Codeigniter Forks of this Blueimp plugin.

The problem was the URL of the DELETE HTTP/AJAX request that the Blueimp plugin specifies by default. Those URLs correspond to a directory path of where the file is uploaded. Unfortunately, Codeigniter by default overrides this by using the URL to determine what controller/controller_method to call.

So for example, my directory structure for the uploaded file is this:

/uploads/img1.jpg

and Codeigniter looked for a controller called uploads and a method called img1.jpg but those obviously didn't exist.

I solved this by changing the Blueimp plugin "upload.class.php" file delete_url that gets assigned to each file. The delete_url was changed from a directory location to a codeigniter controller/controller_method as follows:

protected function set_file_delete_url($file) { 
    $file->delete_url = base_url().'upload/deleteFile/'.rawurlencode($file->name);
    //"upload/deleteFile is my controller/controller_method
    //$file->delete_url = $this->options['upload_url']
    //    .'?file='.rawurlencode($file->name);*/
    //.....

and then here is what my upload/deleteFile function looks like (again following the code nearly verbatim in the Codeigniter Blueimp Fork):

    function deleteFile($file){

    $fcpath=FCPATH.'uploads/;
    $success =unlink($fcpath.$file); //PHP function was does the actual file deletion
            //info to see if it is doing what it is supposed to 
    $info->sucess =$success;
    $info->file =is_file(FCPATH .$file);
    $info->fcpath = FCPATH;
    if (IS_AJAX) {
        //I don't think it matters if this is set but good for error checking in the console/firebug
        echo json_encode(array($info));
    }
    else {     
        //here you will need to decide what you want to show for a successful delete
        $file_data['delete_data'] = $file;
        $this->load->view('admin/delete_success', $file_data); 
     }
}
like image 138
tim peterson Avatar answered Sep 30 '22 14:09

tim peterson


You should only modify this file UploadHandler.php:

function __construct($options = null, $initialize = true, $error_messages = null) {
// Set the following option to 'POST', if your server does not support
// DELETE requests. This is a parameter sent to the client:
//'delete_type' => 'DELETE',
'delete_type' => 'POST',

like image 29
genarocupil Avatar answered Sep 30 '22 15:09

genarocupil