dragon8899 2017-01-04 10:48
浏览 154

$ this-> upload-> do_upload('userfile')无法正常工作

My upload_form.php script

    <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <!--<form action="" method="post">-->

            <?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"/>

       <?php 
       form_close();
       ?>

</body>

Upload.php inside controller

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(['form', 'url']);
    }

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

    public function do_upload()
    {
        $config = [
            'upload_path'   => './uploads/',
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => 100,
            'max_width'     => 1024, //Mainly goes with images only
            'max_heigth'    => 768,
        ];

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

        if (!$this->upload->do_upload('userfile')) {
            $error = ['error' => $this->upload->display_errors()];
            $this->load->view('upload_form', $error);
        } else {
            $data = ['upload_data' => $this->upload->data()];
            $this->load->view('upload_success', $data);
        }
    }
}

When no file select it gives proper error. But on selecting other file (text or image) no error given. Only display blank page

move uploaded file is working.

Upload Success

<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>
  • 写回答

3条回答 默认 最新

  • dsa5233 2017-01-04 11:00
    关注

    try as below..

     public function do_upload()
    {
        $config = [
            'upload_path'   => './uploads/',
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => 100,
            'max_width'     => 1024, //Mainly goes with images only
            'max_heigth'    => 768,
        ];
    
        $this->load->library('upload', $config);
    
        if (!$this->upload->do_upload('userfile')) {
            $error = ['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);
        }
    }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮