Reputation: 24315
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:
Upvotes: 5
Views: 14075
Reputation: 27
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');
}
Upvotes: 0
Reputation: 101
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!
Upvotes: 0
Reputation: 119
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.
Upvotes: 1
Reputation: 51
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',
Upvotes: 3
Reputation: 24315
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);
}
}
Upvotes: 4