2013-10-30 45 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));的东西。