Re: [jQuery-File-Upload] How to rename the uploaded file name?

9,447 views
Skip to first unread message

Tony Abou-Assaleh

unread,
Sep 6, 2012, 11:20:03 PM9/6/12
to jquery-f...@googlegroups.com
I'm guessing you're using the PHP server code. If so, in upload.class.php there is a function "trim_file_name" that you can override to return any file name you like, such as one based on UUID or a hash.

Cheers,
TAA

On Thu, Sep 6, 2012 at 3:58 PM, Aaron Wardle <aaronj...@gmail.com> wrote:
Hi, 

First of all this is a great plugin, and looks perfect for our little project I have it all working on my test server just fine, but we would like the ability to rename the uploaded filename with a random base64 or MD5 file name so that each one is unique and it's also not easy to guess the URL to protect the users uploads. 

Looking at facebook as an example filenames are altered like 423744_524173698397458_518458580.png 

Is there an easy way to make this change without changing a lot of the code? any pointers would be great before I start to try and put this change in. 

Thanks Aaron

--
You received this message because you are subscribed to the Google Groups "jQuery-File-Upload" group.
To post to this group, send email to jquery-f...@googlegroups.com.
To unsubscribe from this group, send email to jquery-fileupl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Aaron Wardle

unread,
Sep 8, 2012, 12:12:29 PM9/8/12
to jquery-f...@googlegroups.com
Thanks for the reply, I ended up overriding the class function with this, which I think is the correct way to do this?

 protected function trim_file_name($name, $type, $index) {
        $name = parent::trim_file_name(md5(date('Y-m-d H:i:s:u')), $type, $index);
        return $name;
    }

Tony Abou-Assaleh

unread,
Sep 9, 2012, 1:15:07 AM9/9/12
to jquery-f...@googlegroups.com
Looks about right to me. Note that md5 doesn't guarantee uniqueness, although the chances of a hash collision under normal circumstances are slim. If you care about filename uniqueness, check these two better options:


Cheers,
TAA

-- 
Tony Abou-Assaleh, PhD
Co-founder and CTO, TitanFile Inc.

151 Charles St. W., Suite 100, Kitchener ON Canada N2G 1H6

Cargo log

unread,
Jul 28, 2015, 5:34:31 PM7/28/15
to jQuery-File-Upload, t...@eits.ca
  protected function trim_file_name($file_path, $name, $size, $type, $error,
            $index, $content_range) {
        // Remove path information and dots around the filename, to prevent uploading
        // into different directories or replacing hidden system files.
        // Also remove control characters and spaces (\x00..\x20) around the filename:
        $name = trim(basename(stripslashes($name)), ".\x00..\x20");
        // Use a timestamp for empty filenames:
        if (!$name) {
            $name = str_replace('.', '-', microtime(true));
        }
        $name=md5($name).".".array_pop(explode(".", $name));
        return $name;
    }


пятница, 7 сентября 2012 г., 6:20:03 UTC+3 пользователь Tony Abou-Assaleh написал:
Reply all
Reply to author
Forward
0 new messages