2013-12-16 1 views
0

두 가지 문제점이 있습니다.임시 파일을 제거하고 drupal에서 업로드를 확인하십시오.

1) 파일 업로드를 테스트 할 때 동일한 파일을 반복해서 업로드합니다. 그러나 drupal은 각 파일의 끝에 증분 번호를 추가합니다. 어느 쪽이 좋습니까?하지만 오래된 파일을 덮어 쓰겠습니다. file_copy()과 다른 방법을 사용해야합니까? drupal이 업로드 된 모든 파일에 대한 내부 참조를 유지한다는 것을 알고 있습니다. 나는 대신 Drupal을 사용할 필요가 없습니다.

if (file_exists) { 
confirmation('do you still want to upload this file?'); 
// if yes foo_file_submit($form, &$form_state, 'confirmed'); 
} else { 
foo_file_submit($form, &$form_state); 
} 

지금까지

function foo_file_validate($form, &$form_state) { 

    $file = file_save_upload('file', array(
    //'file_validate_is_image' => array(), // Validates file is really an image. 
    'file_validate_extensions' => array('jpg'), // Validate extensions. 
)); 
    // If the file passed validation: 
    if ($file) { 

    // Prepare the file to upload 
    $filepath = 'public://foo/files'; 
    file_prepare_directory($filepath, FILE_CREATE_DIRECTORY); // Create the folder if it doesn't exist 

    // Move the file, into the Drupal file system 
    if ($file = file_copy($file, $filepath)) { 
     // Save the file for use in the submit handler. 
     $form_state['storage']['file'] = $file; 
    } 
    else { 
     form_set_error('file', t('Failed to write the uploaded file to the site\'s file folder.')); 
    } 
    } 
    else { 
    form_set_error('file', t('No file was uploaded.')); 
    } 
} 
이 무엇 : 파일 이름이 이미 내가 쓸 수있는 방법이있는 경우

2) 나는 내 자신의 추가 정보를 데이터베이스에 파일 이름을() 추가 해요

답변

0

기존 파일을 덮어 쓰려면 file_copy 함수를 호출 할 때 세 번째 매개 변수를 전달하면됩니다.

기본적으로 Drupal은 FILE_EXISTS_RENAME을 사용하여 파일 이름이 고유 할 때까지 _{incrementing number}을 추가합니다. 귀하의 경우에는

은 다음과 같습니다

file_copy($file, $filepath, FILE_EXISTS_REPLACE) 

당신이 왜 존재하는지 확인하기 위해 파일 이름에 쿼리를 실행하지, 사용자 정의 테이블 (파일 이름)을 이미 uploade 파일을 추가하는 경우? 또는 Drupal과 동일한 논리를 사용할 수 있습니다.

$uri = 'public://yourfile.docx'; 
if (file_exists($uri)) { 
    // Do something 
} 
+0

감사합니다. 하지만 파일이 있으면 confirm_form()을 반환 할 수 있습니까? 나는 그것을 검증 함수에 추가하려고 시도했지만 작동시키지 못한다. – user2988129

관련 문제