2017-04-05 168 views
1

我在前端做了一个窗体来上传一些图像。我的想法是自动重命名上传到唯一ID的所有文件。SilverStripe UploadField文件自动重命名

我曾看过SilverStripe API,但没有看到任何相关内容。 UploadField API

这可能吗?

+1

您是否在SilverStripe论坛上选中了此答案? https://www.silverstripe.org/community/forums/general-questions/show/20232 – 3dgoo

+0

不,我没有看到这一点。谢谢!如果我将UploadField扩展为MyUpload,那么是否必须将所有上传函数与修改一起包含到MyUpload扩展类中?这是否是正确的方法?这是一个很长的功能... – StefGuev

+0

是的,我认为你将不得不这样做,因为你想改变的代码是在函数的中间。这里要记住的一点是,在那个答案中使用的代码将是旧的3.1或3.0上传函数代码。如果你想这样做,你会想复制该功能的最新版本,并将文件重命名添加到它。一个建议,而不是'MyUpload'调用就像'RandomNameUploadField'。 – 3dgoo

回答

2

这是我的解决方案波纹管,在Silverstripe 3.X中,我们必须用另一个类来扩展UploadField。然后将“saveTemporaryFile”功能复制到其中。

就在'尝试 '',只需要添加:

$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]; 

结果:

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



} 

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

0

我现在没有关于一个API,但有一些代码我能够做到这一点。

你有两种可能性。

首先使用数据库。

第二只使用代码:

$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,但你可以看一下大厅的类型。

+0

使用SilverStripe的UploadField时,这并不会自动重命名上传的文件。 – 3dgoo

+0

如果您需要一些帮助,您必须提供一些代码,以便我和其他人员可以提供帮助。 –

+0

对不起,我没有注意到,我没有把代码上传的文件。 –