首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SilverStripe UploadField文件自动重命名

SilverStripe UploadField文件自动重命名
EN

Stack Overflow用户
提问于 2017-04-05 16:44:02
回答 2查看 419关注 0票数 2

我已经做了一个表格在前端上传一些图片。我的想法是自动重命名所有文件上传到唯一的id。

我看过SilverStripe API,没有看到任何关于它的东西。UploadField API

这个是可能的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-07 14:13:56

下面是我的解决方案,在Silverstripe3.x中,我们必须用另一个类扩展UploadField。然后将‘saveTemporaryFile’函数复制到其中。

就在“尝试”之前,只需加上:

代码语言:javascript
复制
$ext = array_reverse(explode('.',$tmpFile['name'])); // explode filename into array, reverse array, first array key will then be file extension
$tmpFile['name'] = hash_hmac('sha256', $tmpFile['name'], '12345') . '.' . $ext[0];

结果:

代码语言:javascript
复制
class RandomNameUploadField extends UploadField {


protected function saveTemporaryFile($tmpFile, &$error = null) {

    // Determine container object
    $error = null;
    $fileObject = null;

    if (empty($tmpFile)) {
        $error = _t('UploadField.FIELDNOTSET', 'File information not found');
        return null;
    }

    if($tmpFile['error']) {
        $error = $tmpFile['error'];
        return null;
    }

    // Search for relations that can hold the uploaded files, but don't fallback
    // to default if there is no automatic relation
    if ($relationClass = $this->getRelationAutosetClass(null)) {
        // Create new object explicitly. Otherwise rely on Upload::load to choose the class.
        $fileObject = Object::create($relationClass);
    }

    $ext = array_reverse(explode('.',$tmpFile['name'])); // explode filename into array, reverse array, first array key will then be file extension
    $tmpFile['name'] = hash_hmac('sha256', $tmpFile['name'], '12345') . '.' . $ext[0];

    // Get the uploaded file into a new file object.
    try {
        $this->upload->loadIntoFile($tmpFile, $fileObject, $this->getFolderName());
    } catch (Exception $e) {
        // we shouldn't get an error here, but just in case
        $error = $e->getMessage();
        return null;
    }

    // Check if upload field has an error
    if ($this->upload->isError()) {
        $error = implode(' ' . PHP_EOL, $this->upload->getErrors());
        return null;
    }

    // return file
    return $this->upload->getFile();
}



}

谢谢@3 3dgoo给我的部分解决方案!

票数 2
EN

Stack Overflow用户

发布于 2017-04-05 23:37:16

我现在并不是关于API的,但是有了一些代码,我就能够做到这一点。

你有两种可能性。

首先使用数据库。

第二,仅使用代码:

代码语言:javascript
复制
$directory = '/teste/www/fotos/';
$files = glob($directory . '*.jpg');

if ( $files !== false )
{
    $filecount = count( $files );
    $newid = $filecount+1;
    $new_name = "foto_".$newid;
    $target_file =  $directory."/".$new_name;
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

}
else
{
     $new_name = "foto_1";
     $target_file =  $directory."/".$new_name;
     move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}

我的例子是jpeg,但是您可以查找hall类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43237050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档