“Blueimp jQuery File Upload” rename files

后端 未结 7 2025
挽巷
挽巷 2021-02-19 15:22

I\'m using the Blueimp jQuery file upload tool. I\'d like to completely rename the files as they\'re uploaded. Since photos are being added to a unique directory based on the us

相关标签:
7条回答
  • 2021-02-19 15:43

    Thanks for that !

    But I got a problem, I would like to rename with a field title, I've try to use that :

    protected function handle_form_data($file, $index) {
        $file->title = @$_REQUEST['title'][$index];
    }
    protected function generate_unique_filename($filename = "") {
        $extension = "";
        if ( $filename != "" )
        {
            $extension = pathinfo( $filename, PATHINFO_EXTENSION );
            if ( $extension != "" ) {
                $extension = "." . $extension;
            }
        }
        return $file->title . $extension;
    }
    

    But I got a JSON error everytime... (The @$_REQUEST['title'][$index]; work well, I use it for save it in my database)

    0 讨论(0)
  • 2021-02-19 15:45

    get_file_name method is where the actual naming occurs. In order to avoid much workarounds and just hit the nail on the head, edit it as follows:

    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);
        //don't return this right away; parse it to a variable
        $unique_file_name = $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
        );
        //generate your custom name here
        $custom_name = uniqid('profilepic');
        //extract file extension
        $file_extension = pathinfo($unique_file_name, PATHINFO_EXTENSION);
        return $custom_name.'.'.$file_extension;
    }
    
    0 讨论(0)
  • 2021-02-19 15:51

    Why not trying uniqid() to generate an unique name?

    protected function generate_unique_filename( $filename = "" )
    {
        $extension = "";
        if ( $filename != "" )
        {
            $extension = pathinfo( $filename, PATHINFO_EXTENSION );
    
            if ( $extension != "" )
            {
                $extension = "." . $extension;
            }
        }
        return md5( uniqid() ) . $extension;
        //or simply return uniqid() . $extension;
    }       
    
    0 讨论(0)
  • 2021-02-19 15:52

    For multiple files download, I add some lines from SananTheBest as following. Actually, The reason files can not upload because the files name are same even using date as mentioned in his pervious post. (Thanks you)

    protected function generate_unique_filename($filename = "")
    {
    
        $extension = "";
        if ( $filename != "" )
        {
            $extension = pathinfo($filename , PATHINFO_EXTENSION);
    
            if ( $extension != "" )
            {
                $extension = "." . $extension;
            }
        }
        //$filenamegen = date('ymdHis').generateRandomString();
        $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $charactersLength = strlen($characters);
        $randomString = "";
        for ($i = 0; $i < 5 ; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
    
    
        //$filenamerandom=$filename.".jpg";
        //return md5(date('Y-m-d H:i:s:u')). $extension;
    
        return date('ymdHis').$randomString . $extension ;
    }
    
    0 讨论(0)
  • 2021-02-19 15:54

    Old post, but as I was searching the same thing today, I post my solution hoping it will help someone.

    I edited the UploadHandler.php file in this way:

            //custom function which generates a unique filename based on current time
            protected function generate_unique_filename($filename = "")
                {
    
                    $extension = "";
                    if ( $filename != "" )
                    {
                        $extension = pathinfo($filename , PATHINFO_EXTENSION);
    
                        if ( $extension != "" )
                        {
                            $extension = "." . $extension;
                        }
                    }
    
                    return md5(date('Y-m-d H:i:s:u')) . $extension;
                }
    
    
                protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
                        $index = null, $content_range = null) {
                    $file = new \stdClass();
    
                    //MYEDIT to generate unique filename
                    $file->name = $this->generate_unique_filename($name);
    
                    //ORIGINAL LINE: $file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);
    
                    //...
    }
    
    0 讨论(0)
  • 2021-02-19 15:54

    edit the $name variable in UploadHandler class to get the unique file name. See the code

    protected function get_file_name($file_path, $name, $size, $type, $error,$index,$content_range)
    
    0 讨论(0)
提交回复
热议问题