Howto call own php functions in UploadHandler.php after a successful upload?

762 views
Skip to first unread message

Michael Marre

unread,
Nov 14, 2013, 3:13:35 AM11/14/13
to jquery-f...@googlegroups.com
Hi all,

I am not a crack in JS and I currently have issues to understand the workflow between jquery.fileupload.js and UploadHandler.php.

The situation:

I would like to use jquery upload for uploading files only. Files should not be downloadable after a successful upload and finally the uploaded files should be
moved out of the webserver accessible directory into a directory somewhere where a cron job will pick up the file and do further action on this (virus scan, sort it into a defined directory structure as per naming convetion of the file).
This is for security reasons that no one can upload a malicious file and link it on hacked websites which would be possible as long the file is readable in the webserver directory. Furthermore the file should be archived in a special directory structure for further internal processing.
So you can understand this as a "Mailbox" where User can "anonymously" drop files one way but should never be able to get the files downloaded.

Hint: maxChunkSize is set to 10 MB (which seemly results in many repeating calls of filesize() during upload of one large file)


Target is to rename the uploaded file based on additional formular fields and move it out to a specific location. Renaming and moving the file is working fine with the following code

The problem:

Using the following code (focus on // Manipulate filename and // Moving file...) I am able to rename the file and move it out into the $TARGET_DIR.

    protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
            $index
= null, $content_range = null) {
        $file
= new stdClass();
        $file
->name = $this->get_file_name($name, $type, $index, $content_range);
        $file
->size = $this->fix_integer_overflow(intval($size));
        $file
->type = $type;
       
if ($this->validate($uploaded_file, $file, $error, $index)) {
            $this
->handle_form_data($file, $index);
            $upload_dir
= $this->get_upload_path();
           
if (!is_dir($upload_dir)) {
                mkdir
($upload_dir, $this->options['mkdir_mode'], true);
           
}
            $file_path
= $this->get_upload_path($file->name);
            $append_file
= $content_range && is_file($file_path) &&
                $file
->size > $this->get_file_size($file_path);
           
if ($uploaded_file && is_uploaded_file($uploaded_file)) {
               
// multipart/formdata uploads (POST method uploads)
               
if ($append_file) {
                    file_put_contents
(
                        $file_path
,
                        fopen
($uploaded_file, 'r'),
                        FILE_APPEND
                   
);
               
} else {
// Manipulate filename
           
global $CSS_NO, $CSS_YEAR, $UNIT, $TARGET_DIR;
            $file_nam
= $this->get_file_name($name, $type, $index, $content_range);
            $FILENAME
=$CSS_NO . "_" . $CSS_YEAR . "." . $UNIT . "." . $file_nam;
            $filepath
=$TARGET_DIR . $FILENAME;

                    move_uploaded_file
($uploaded_file, $file_path);                    

// Moving file and delete from incoming

           
if (copy($file_path, $filepath)) {
                unlink
($file_path);
           
}

But then the file is missing in the original upload directory and jquery file upload displays errors because of the file is missing in the standard jquery file upload directory.

Reading the documentation I understand that I have to use the DONE callback of jquery.fileupload.js. But I am unfortunately not a JS crack. So my question after the long story is:

How I have to call a function in UploadHandler.php (let's call it rename_moveout() for example) in the callback

            // Callback for successful uploads:
           
// done: function (e, data) {}, // .bind('fileuploaddone', func);


The php function will only require $file_path (jquery variable with the full qualified location of the uploaded file) and $filepath (full qualified location of the target directory).

Any help will be really appreciated.

Regards,
Michael

Reply all
Reply to author
Forward
0 new messages