tim*_*son 5 javascript php codeigniter file-upload delete-file
我正在尝试使用这个非常好的jQuery Blueimp文件上传插件删除文件.
我把这个插件放在我的根目录中,并且能够上传和删除文件没问题.
但是,当我在我的codeigniter应用程序中嵌入此插件时,由于405错误,我不再删除我上传的文件.我已将所有文件夹设置为777只是为了确保这不是问题.
有什么想法吗?这是我的控制台日志:

我按照这个 Blueimp 插件的Codeigniter Forks 之一中的代码解决了我自己的问题。
问题在于 Blueimp 插件默认指定的 DELETE HTTP/AJAX 请求的 URL。这些 URL 对应于上传文件的目录路径。不幸的是,Codeigniter 默认情况下通过使用 URL 来确定要调用的控制器/controller_method 来覆盖这一点。
例如,我上传文件的目录结构是这样的:
/uploads/img1.jpg
Run Code Online (Sandbox Code Playgroud)
Codeigniter 寻找一个被调用的控制器uploads和一个被调用的方法img1.jpg,但这些显然不存在。
delete_url我通过更改分配给每个文件的Blueimp 插件“upload.class.php”文件解决了这个问题 。delete_url从目录位置更改为 codeigniter 控制器/controller_method,如下所示:
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);*/
//.....
Run Code Online (Sandbox Code Playgroud)
然后这是我的函数的样子(再次几乎逐字地遵循Codeigniter Blueimp Forkupload/deleteFile中的代码):
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);
}
}
Run Code Online (Sandbox Code Playgroud)