删除文件时遇到问题,这是因为dropzone表单上的文件名与文件夹中的文件名不同。 因此,我无法删除该图像。但是如果图像名称相同,我可以将其删除。
如果您问为什么名称会更改?因为我在控制器上自动更改了名称。
我的控制器
public function create(){
if (!empty($_FILES)) {
$config['upload_path'] = $this->upload_path;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$new_name = time().$_FILES["file"]['name'];
$config['file_name'] = $new_name;
$config['width'] = 900;
$config['height'] = 600;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file')){
$uploadData = $this->upload->data();
$foto = $uploadData['file_name'];
}
}
public function remove()
{
$file = $this->input->post("file");
if(file_exists($this->upload_path.$file)) {
unlink($this->upload_path.$file);
}
}
}
我的JavaScript
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("#dropzoneForm", {
url: "<?= base_url() ?>backend/galeri/create",
acceptedFiles: "image/*",
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
$.ajax({
type: "post",
url: "<?= base_url() ?>backend/galeri/remove",
data: { file: name },
dataType: 'html'
});
var previewElement;
return (previewElement = file.previewElement) != null ? (previewElement.parentNode.removeChild(file.previewElement)) : (void 0);
}
});
我的表格
<form action="<?= base_url() ?>backend/galeri/create" class="dropzone" id="dropzoneForm" enctype="multipart/form-data">
</form>