2017-04-05 7 views
1

일부 이미지를 업로드하기 위해 프런트 엔드에 양식을 만들었습니다. 내 아이디어는 자동으로 고유 한 ID에 업로드 된 모든 파일의 이름을 바꾸는 것입니다.SilverStripe UploadField 파일 자동 이름 바꾸기

나는 SilverStripe 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

죄송합니다. 파일을 업로드하기위한 코드를 넣지 않았 음을 눈치 채지 못했습니다. –