"Blueimp jQuery文件上传"重命名文件 [英] "Blueimp jQuery File Upload" rename files
问题描述
我正在使用Blueimp jQuery文件上传工具.我想在上传文件时完全重命名它们.由于照片是根据用户ID添加到唯一目录中,因此我真的只想添加名称为001.jpg,002.jpg,003.jpg等的照片.如何做到这一点(可以通过修改UploadHandler来实现). php或index.php?
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 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?
在index.php中(下面的代码当前会更改上传目录):
In index.php (code below currently changes the upload directory):
$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.
我以这种方式编辑UploadHandler.php文件:
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);
//...
}
这篇关于"Blueimp jQuery文件上传"重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!