2013-10-30 44 views
0

我剛開始學習Drupal 7,想用自定義表單上傳文件。但是當我上傳文件時,會產生以下錯誤。 enter image description here在drupal上傳文件時出現錯誤7

這是我的代碼。

function custom_form_form($form,&$form_state) { 
    $form = array(); 
     $form['photos'] = array(
     '#title' => t('Image'), 
     '#type' => 'file', 
     '#name' => 'files[photos]', 
    ); 
    $form['submit'] = array(
      '#value' => 'Submit', 
      '#type' => 'submit', 
      '#name' => 'submit', 
    );          
    $form['#submit'][] = 'custom_submit_function'; 
    return $form; 
} 

function custom_submit_function($form, &$form_state){ 

    $validators = array(
     'file_validate_extensions' => array('jpg png gif'), 
    ); 
    //Save file 
    $file_destination = "public://Photos/"; 
    $file = file_save_upload('photos', $validators, $file_destination,FILE_EXISTS_RENAME); 
    if(isset($file->uri)){ //if you need this file to be not temporary 
      $file->status = 1; 
      file_save($file); 
    } 
    if ($file) { 
      $file_content = file_get_contents($file->filepath); 
      echo $file_content; 
    } 
    else{ 
     print_r(form_set_error('photos', 'Could not upload file.')); 
    } 
} 

我不知道我是否犯錯!!!

+0

發表於http://drupal.stackexchange.com/ –

回答

1

如果你看Drupal 7 API中的file_save_upload()的定義,看起來該函數返回的'file'對象沒有'filepath'成員。您可能想嘗試使用類似$file_content = file_get_contents(file_create_url($file->uri));的東西。