0

我正在使用 CodeIgniter 文件上传类。

我无法将错误数组加载到文件上传表单视图中。(这将返回“未定义变量:错误”消息或“数组到字符串转换”错误消息)

而且我也无法将upload_data 数组加载到成功视图中。(这将返回“数组到字符串转换”错误消息。)

这是我的控制器 - upload.php

<?php

class Upload extends  Admin_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));

        $this->load->helper('directory');
     }

    function index()
    {

        $this->data['subview'] = 'admin/upload_form';
        $this->load->view('admin/_layout_main', $this->data);

    }

    function do_upload()
    {


        $this->load->library('upload');

        if ( ! $this->upload->do_upload())
        {

            $error = array('error' => $this->upload->display_errors());

            $this->data['error'] = $error;


            $this->data['subview'] = 'admin/upload_form';
            $this->load->view('admin/_layout_main', $this->data);


         }
         else
         {
            $upload_data = array('upload_data' => $this->upload->data());

            $this->data['upload_data'] = $upload_data;

            $this->data['subview'] = 'admin/upload_success' ;


            $this->load->view('admin/_layout_main', $this->data);
         }
      }
  }

  ?>

这是上传表单视图upload_form.php

<?php  echo $error;?>

<?php echo form_open_multipart('admin/upload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

这是成功页面视图

 <ul>
  <?php foreach ($upload_data as $item => $value):?>
  <li><?php echo $item;?>: <?php echo $value;?></li>
      <?php endforeach; ?>
</ul>

 <p><?php echo anchor('admin/upload', 'Upload Another File!'); ?></p>
4

1 回答 1

1
$upload_data = array('upload_data' => $this->upload->data());
$this->data['upload_data'] = $upload_data;

这意味着当您在视图中 print_r $upload_data 时,其中有一个数组,其中的键为“upload_data”,另一个数组作为值。然后当你 echo $value 它返回一个错误,因为它是一个数组而不是一个字符串。

我相信您正在寻找的只是:

$this->data['upload_data'] = $this->upload->data();
于 2013-10-14T12:28:04.820 回答