jQuery 文件上传自定义 get_file_objects 函数无法从数据库导入数据

jQuery File Upload custom get_file_objects function not working to import data from database

我扩展了 jquery 插件 blueimp / jQuery-File-Upload 的 UploadHandler class。我有一个自定义 handle_file_upload 函数,如下所示。

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
        $index = null, $content_range = null) {
    $file = parent::handle_file_upload(
        $uploaded_file, $name, $size, $type, $error, $index, $content_range
    );
    $file->unique_code = random_string(32);
    $file->files_locations = $this->get_file_objects();
    $file->files_locations_parent = parent::get_file_objects();
    if($file->files_locations === $file->files_locations_parent):
        $file->files_same = 'Yes';
    elseif($file->files_locations == $file->files_locations_parent):
        $file->files_same = 'Nearly';
    else:
        $file->files_same = 'No';
    endif;
    $file->email_id = $_REQUEST['email_id'];
    if (empty($file->error)) {
        $email_date = array();
        $email_date['filename'] = $file->name;
        $email_date['size'] = $file->size;
        $email_date['type'] = $file->type;
        $email_date['unique_code'] = $file->unique_code;

        db_insert($this->table, $email_date);
    }
    return $file;
}

这个 class 和函数运行良好,但是当我尝试引入一个自定义 get_file_objects 函数时它表现不正常。

protected function get_file_objects($iteration_method = 'get_file_object') {
    $upload_dir = $this->get_upload_path();
    if (!is_dir($upload_dir)) {
        return array();
    }
    return array_values(array_filter(array_map(
        array($this, $iteration_method),
        // $this->get_filenames($_REQUEST['email_id'])
        scandir($upload_dir)
    )));
}

正如上面打印的那样工作正常,但这仅 returns 与父 class 相同。当我注释掉 scandir 并取消注释另一个时,我没有显示任何文件。

我知道 get_file_objects 函数正在运行,因为我将输出附加到 handle_file_upload 函数中的文件对象,然后将其显示在客户端的控制台中。这是代码片段和输出。

$file->files_locations = $this->get_file_objects();
$file->files_locations_parent = parent::get_file_objects();
if($file->files_locations === $file->files_locations_parent):
    $file->files_same = 'Yes';
elseif($file->files_locations == $file->files_locations_parent):
    $file->files_same = 'Nearly';
else:
    $file->files_same = 'No';
endif;

数据库中的已知文件与文件系统中的文件不同,对象不一样,但我相信结构是相同的:

files_locations
:
Array(1)
0
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.41.png"
name
:
"Screen Shot 2018-01-08 at 15.44.41.png"
size
:
14246
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
__proto__
:
Object
length
:
1
__proto__
:
Array(0)
files_locations_parent
:
Array(4)
0
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.03.png"
name
:
"Screen Shot 2018-01-08 at 15.44.03.png"
size
:
107418
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.03.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.03.png"
__proto__
:
Object
1
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.41.png"
name
:
"Screen Shot 2018-01-08 at 15.44.41.png"
size
:
14246
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.41.png"
__proto__
:
Object
2
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.45%20%281%29.png"
name
:
"Screen Shot 2018-01-08 at 15.44.45 (1).png"
size
:
14230
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.45%20%281%29.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.45%20%281%29.png"
__proto__
:
Object
3
:
deleteType
:
"DELETE"
deleteUrl
:
"http://localhost/amdev/public_html/email_attachment.php?file=Screen%20Shot%202018-01-08%20at%2015.44.45.png"
name
:
"Screen Shot 2018-01-08 at 15.44.45.png"
size
:
14230
thumbnailUrl
:
"http://localhost/amdev/public_html/files/thumbnail/Screen%20Shot%202018-01-08%20at%2015.44.45.png"
url
:
"http://localhost/amdev/public_html/files/Screen%20Shot%202018-01-08%20at%2015.44.45.png"
__proto__
:
Object
length
:
4
__proto__
:
Array(0)

启动文件上传器的Javascript代码如下:

$('#email-form').fileupload({
    url: HOME + 'email_attachment.php',
    formData: {email_id: email_id}
});

在 WHERE 子句中使用 email_id 来 select 来自服务器的所需附件。

任何关于为什么这对我不起作用的帮助将不胜感激。

谢谢,

斯图

我找到了解决方案。问题不在于 PHP,而在于 JavaScript。在 JS 中初始化 fileupload 时,它似乎不发送 formData,这仅在文件上传发生时发生。因此,我只是改变了向服务器提交数据的方式,而不是使用 formData 和相应的 $_REQUEST 我创建了一个 Get 调用和 $_GET。下面的代码。

$('#email-form').fileupload({
    url: HOME + 'email_attachment.php?email_id=' + email_id
})