2017-04-05 15 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についてはいませんが、いくつかのコードで私はそれを行うことができました。

あなたには2つの可能性があります。

最初にデータベースを使用しています。コードのみを使用して

第二:

$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

申し訳ありません私はファイルをアップロードするためのコードを書いていないことに気づいていません。 –

関連する問題