0

我正在尝试使用 codeigniter 上传照片,但这不起作用。我没有得到文件名,它只显示错误。我的系统文件夹中也有帮助文件,但我不上传照片。

控制器:upload.php

<?php
     class Upload extends CI_Controller {
        function __construct()
   {
    parent::__construct();
    $this->load->helper(array('form', 'url','file'));
    }

    function index()
    {
    $this->load->view('upload_form', array('error' => ' ' ));
     }

    function do_upload()
    {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config); 
    if ( ! $this->upload->do_upload())
    { 
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    { 
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
     }
     }
 ?>

查看:upload_form.php

<html>
   <head>
       <title>Upload Form</title>
   </head>
   <body>
        <?php echo $error;?>
         <?php echo form_open_multipart('upload/do_upload');?>
         <input type="file" name="userfile" size="20" />
            <br /><br />
            <input type="submit" value="upload" />
              </form>
       </body>
 </html>

上传成功.php

<html>
   <head>
      <title>Upload Form</title>
   </head>
   <body>
          <h3>Your file was successfully uploaded!</h3>
                 <ul>
              <?php foreach ($upload_data as $item => $value):?>
                 <li><?php echo $item;?>: <?php echo $value;?></li>
              <?php endforeach; ?>
          </ul>
           <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
              </body>
     </html>
4

1 回答 1

0

我总是在调用 do_upload() 方法时指定输入字段名称。这是来自工作示例的片段,遵循模式,它应该可以工作。

$field_name = 'userfile';
if (isset($_FILES[$field_name]) && is_uploaded_file($_FILES[$field_name]['tmp_name'])) {
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload($field_name)) {
        //upload failed
    } else {
        //upload passed
    }
}

另外,请确保您的上传目录是可写的。

于 2013-11-01T10:19:16.570 回答