phpjqueryfileuploadblueimp

Blueimp custom file names


How do you get Blueimp jquery file uploader to give custom file names to files which are stored in sessions?

I tried adding this to index.php

class CustomUploadHandler extends UploadHandler
{
    protected function trim_file_name(
        $file_path,
        $name,
        $size,
        $type,
        $error,
        $index,
        $content_range
    ) {
        $name = 'your-custom-file-name'.".\x00..\x20";
        // Use a timestamp for empty filenames:
        if (!$name) {
            $name = str_replace('.', '-', microtime(true));
        }
        return $name;
    }
}

$upload_handler = new CustomUploadHandler();

but I got a json empty whitespace error.


Solution

  • I did something like this and it works perfectly

    find get_file_name() declaration and do changes with what you are returning. what ever you return will be the the file name, don't forget to consider returning the file extension.. In my case I have used a rand file name with the same extension as that of the uploaded file name...

    protected function get_file_name($file_path, $name, $size, $type, $error,
        $index, $content_range) {
        $name = $this->trim_file_name($file_path, $name, $size, $type, $error,
        $index, $content_range);
        $filen = $this->get_unique_filename(
        $file_path,
        $this->fix_file_extension($file_path, $name, $size, $type, $error,
        $index, $content_range),
        $size,
        $type,
        $error,
        $index,
        $content_range
        );
        $pieces = pathinfo($filen);
        $md5filename = substr(md5(time()), 0, 15);
        $md5filename .= '.'.$pieces['extension'];
        return $md5filename;
        }