【问题标题】:Getting error when upload file in drupal 7在drupal 7中上传文件时出错
【发布时间】:2013-10-30 12:14:19
【问题描述】:

我刚开始学习 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.'));
     }
}

我不知道我在哪里犯错了!!!

【问题讨论】:

标签: file-upload drupal-7


【解决方案1】:

如果您查看Drupal 7 APIfile_save_upload() 的定义,该函数返回的“文件”对象似乎没有“文件路径”成员。您可能想改用$file_content = file_get_contents(file_create_url($file->uri)); 之类的方法。

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多