CI上传文件 并出输出上传后的路径

1、创建表单 upload_form.php

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<form action="<?php echo site_url('upload/do_upload') ?>"  method="post" class="form-horizontal" enctype="multipart/form-data">



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

<br /><br />

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


</form>



</body>
</html>

2、写上传程序upload.php

<?php
/*constructor function to initialize controller and load the file upload class,
 plus the two other helpers it needs */
class Upload extends CI_Controller {
    function Upload()
    {
          //parent::Controller();
      parent::__construct(
        );  
       
         

    }
    /*now the function which does all the work!*/
    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', $error);

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

      }
      

      // $this->upload->do_upload();      
      // $data = array('upload_data' => $this->upload->data());
      // $this->load->view('upload_success', $data);

    }
}

3、调用  在welcome入口控制器 调用视图upload_form

public function index()
	{
		// $this->load->view('header');
		// $this->load->view('index');
		// $this->load->view('footer');
		$this->load->view('upload_form');
	}

4、上传成功的页面,并输出上传后的文件路径  upload_success.php

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</h3>



<ul>
<?php foreach($upload_data as $item => $value):?>
<?php if($item=='full_path'){
	echo $value;
} 
	
?>


<?php endforeach; ?>
</ul>

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

</body>
</html>

5、上传失败的显示页面 upload_form_error.php


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜗牛慢慢向上爬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值