最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DELETE method on jQuery Blueimp FileUpload not allowed using CodeigniterPHP (405 error)? - Stack Overflow

programmeradmin3浏览0评论

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:

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:

Share Improve this question edited Aug 23, 2015 at 13:52 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 8, 2012 at 18:25 tim petersontim peterson 24.3k63 gold badges184 silver badges302 bronze badges 6
  • 1 That's not a filesystem permissions issue - your webserver is not allowing DELETE actions. – Marc B Commented Aug 8, 2012 at 18:27
  • i'm using Apache. If this is a webserver issue why is it allowing DELETE outside of my codeigniter app but not inside of it? – tim peterson Commented Aug 8, 2012 at 18:29
  • 1 Check for a <limit> directive somewhere. Just because it works in one place but not another doesn't mean much - apache allows overrides on all sorts of levels - per vhost, per dir, per url, blah blah blah. – Marc B Commented Aug 8, 2012 at 18:32
  • 1 Worth checking in on handling alternative HTTP verbs w/ CI: stackoverflow./questions/5540781/… – rjz Commented Aug 8, 2012 at 18:33
  • @MarcB can you be more specific on where I should look for the <limit> directive? I'm not familiar with that. – tim peterson Commented Aug 8, 2012 at 18:55
 |  Show 1 more ment

5 Answers 5

Reset to default 4

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); 
     }
}

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',

I solved it, in the following way:

in the file: UploadHandler.php, in the constructor

Before:

$this->options = array(
        'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),

Add to the end my load control method

$this->options = array(
        'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')).'/cargar'

Just add the route pointing to the method you created to upload your files.

I added the jquery file upload to a controller in my application. Like Mark, I could not get the files deleted. But I solved the problem like this:

In the constructor, the first element of the options array

'script_url' => $this->get_full_url().this->basename($this->get_server_var('SCRIPT_NAME')),

remove the reference to SCRIPT_FILENAME

remove the reference to SCRIPT_FILENAME and add the name of the driver that loads the uploadhandler.php library. In my case, fileupload

'script_url' => $this->get_full_url().'/fileupload/',

Ready. Just try!

All you need to do is change the "script_url" inside "UploadHandler" and point it to where your "UploadHandler" library is called.

//BEFORE

$this->options = array(
'script_url'=>$this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),



//AFTER

$this->options = array(
'script_url'=>base_url().'admin/categories/category_manage/picturesupload'



Because i am using bonfire-CI thats why "admin" for admin panel
"categories" is the name of context
"category_manage" is the name of module
"picturesupload" is the name of method in which i am loading the libarary

If you are not using bonfire then simply change it to "your controller name"/"your method name where the UploadHandler libaray is called"
//"picturesupload" method look like this

public function picturesupload(){

        error_reporting(E_ALL | E_STRICT);
        $this->load->library('UploadHandler');

}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论