简体   繁体   English

“ Blueimp jQuery File Upload”重命名文件

[英]“Blueimp jQuery File Upload” rename files

I'm using the Blueimp jQuery file upload tool. 我正在使用Blueimp jQuery文件上传工具。 I'd like to completely rename the files as they're uploaded. 我想在上传文件时完全重命名它们。 Since photos are being added to a unique directory based on the userID, I'd really just like to add photos with names 001.jpg, 002.jpg, 003.jpg, etc. How can I accomplish this (either by modifying UploadHandler.php or index.php? 由于照片是根据用户ID添加到唯一目录中,因此我真的只想添加名称为001.jpg,002.jpg,003.jpg等的照片。如何做到这一点(可以通过修改UploadHandler来实现)。 php或index.php?

In index.php (code below currently changes the upload directory): 在index.php中(下面的代码当前会更改上传目录):

$userID = 'user005'; //hardcoded in this example
$options = array('upload_dir'=>'files/'.$userID.'/', 'upload_url'=>'files/'.$userID.'/');
$upload_handler = new UploadHandler($options);

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: 我以这种方式编辑UploadHandler.php文件:

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

                //...
}

For those who only what to give the name through the $options : 对于那些仅通过$options给出name的人:

Replace (line 1056?) 替换(1056行?)

$file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);

with

$extension = pathinfo($name , PATHINFO_EXTENSION);
if ( $extension != "" ){
    $extension = "." . $extension;
}

$file->name = (isset($this->options['filename']) && isset($extension) ) 
     ? $this->options['filename'] . $extension
     : $this->get_file_name($uploaded_file, $name, $size, $type, $error, $index, $content_range);

Then give the name in the option like this: 然后在这样的选项中输入名称:

$upload_handler = new UploadHandler(array(['filename' => 'whatever' ]));

Why not trying uniqid() to generate an unique name? 为什么不尝试uniqid()生成唯一名称?

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

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) 但是我每次都会收到JSON错误...(@ $ _ REQUEST ['title'] [$ index];运行良好,我将其保存在数据库中)

edit the $name variable in UploadHandler class to get the unique file name. UploadHandler类中编辑$name变量以获取唯一的文件名。 See the code 看代码

protected function get_file_name($file_path, $name, $size, $type, $error,$index,$content_range)

For multiple files download, I add some lines from SananTheBest as following. 对于多个文件下载,我从SananTheBest添加一些行,如下所示。 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 ;
}

get_file_name method is where the actual naming occurs. get_file_name方法是实际命名的地方。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM