简体   繁体   English

blueimp jquery文件上传-使用数据输入(如标题)重命名文件名

[英]blueimp jquery file upload - rename filename with data input like title

I would like to rename the filename with a hidden additional form field followed by a small unique random number while uploading the photo. 我想在上传照片时使用一个隐藏的附加表单字段重命名文件名,后跟一个小的唯一随机数。 The file name should look like this: hidden_form_field-random_number.jpg = hello_Dj84lx.jpg 文件名应如下所示:hidden_​​form_field-random_number.jpg = hello_Dj84lx.jpg

I have found solutions to do random names and so on but not with an inserted form. 我已经找到了解决方法来做随机名称等等,但是没有插入形式。 I have tried to use $file->title from the examples but without success. 我试图从示例中使用$ file-> title,但没有成功。

I managed to solve the problem. 我设法解决了这个问题。

First of I added this function to the upload/server/php/index.php file 首先,我将此功能添加到upload / server / php / index.php文件中

protected function generate_unique_filename($filename = ""){
        $extension = "";
        if ( $filename != "" ){
            $extension = pathinfo($filename , PATHINFO_EXTENSION);
            if ( $extension != "" ){
                $extension = "." . $extension;
            }
        }           
        $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $charactersLength = strlen($characters);
        $randomString = "";
        for ($i = 0; $i < 5 ; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $filename.'_'.date('siHdmy').$randomString.$extension;
}

Then I edited the UploadHandler.php file. 然后,我编辑了UploadHandler.php文件。 I added a public variable $mytitle and replaced this code: 我添加了一个公共变量$ mytitle并替换了以下代码:

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

with this 有了这个

$this->mytitle='what-ever-text-filled-in-the-title-form-field';
$file->name = $this->generate_unique_filename($this->mytitle);

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

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