Search code examples
javascriptphpjqueryfile-uploadjquery-file-upload

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


I've extended the UploadHandler class of the jquery plugin blueimp / jQuery-File-Upload. I have a custom handle_file_upload function as below.

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

This class and function work well, but when I try to introduce a custom get_file_objects function it misbehaves.

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

As it is printed above works fine, but this only returns the same as the parent class. When I comment out the scandir and uncomment the other I get no files displayed.

I know the get_file_objects function is working because I attach the output to the file object in the handle_file_upload function and then display it in the console on the client side. Here is the code snippet and output.

$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;

With known different files from the database as apposed to the filesystem the objects at not the same but I believe the structure to be the same:

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)

The Javascript code to initiate the File Uploader is as follows:

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

Where the email_id is used in a WHERE clause to select the required attachments from the server.

Any help as to why this isn't working for me would be much appreciated.

Thanks,

Stu.


Solution

  • I found the solution. The problem wasn't with the PHP, it was with the JavaScript. When fileupload is initialised in JS, it doesn't seem to send the formData, this only occurs when a file upload takes place. Therefore I simply changed the way I submitted the data to the server, instead of using the formData and corresponding $_REQUEST I created a Get call and $_GET. Code below.

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