【问题标题】:Not able to upload a file in code igniter.无法在 codeigniter 中上传文件。
【发布时间】:2017-01-26 19:37:54
【问题描述】:

我正在尝试使用代码点火器上传图像,但它没有显示任何成功失败消息。 请帮忙。

这是一个文件上传表单
查看- Upload_form.php

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

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

这是查看成功文件
查看 - upload_success.php

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Upload form</title>
    </head>
    <body>
        <h1> YOUR FILE WAS SUCCESSFULLY UPLOADED </h1>
        <ul>
  <?php foreach ($upload_data  as $item => $value); ?>          

            <li><?php echo $item; ?> : <?php echo $value; ?></li>
            <?phpendforeach ;?>

        </ul>

   <?php echo anchor('upload','upload Another file'); ?>     
    </body>
</html>

这是一个控制器文件。
控制器 - upload.php

<?php

class Upload extends CI_Controller {

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

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

    }

    public function do_upload(){

       $config = array(  
              '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 = 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);

    }


    }

}

我已经在代码点火器的根目录中创建了上传文件夹。

【问题讨论】:

标签: codeigniter file-upload


【解决方案1】:

请对控制器和查看页面的代码进行一些更改。同时确保您的uploads文件夹位于之外>application folder.below 代码用于您的视图 Upload_form

<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>

并在您的控制器中尝试以下代码上传

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

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

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

    }

    public function do_upload(){

       $config = array(  
              '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 = 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);

    }


    }

}

【讨论】:

  • 在 route.php 中定义路由是否重要,例如 $route['upload_path'] = 'Upload'; .这个有什么用。
【解决方案2】:

您的 upload_path 可能不正确,所以这是我正在使用的代码:

1.在你的控制器顶部添加这个来声明upload_path:

function __construct()
{   
  parent::__construct();
  $this->uploadPath = realpath(APPPATH . '../uploads');
}

2.更改 $config upload_path 从

'./uploads/'

到

$this->上传路径


更多详情请看我的uploadProfile Controller:

function upload_profilePic($userID)
    {
        //create a unique ID
        $filename = $userID. '-' .uniqid();

        //config upload
            $config=array(
                'allowed_types' =>  'jpg|jpeg|png|PNG',
                'upload_path'   =>  $this->imagePath,
                'max_size'  =>  90000,
                'file_name' =>  $filename
            );
        //upload
            $this->load->library('upload',$config);
            $this->upload->do_upload('profilePic');

        //get file data
            $data = $this->upload->data();
            $fileExtension = $data['file_ext'];

        if($fileExtension=="")
            $filename = '';
        else
            $filename=$data['file_name'];

        //
        $data = array(
            'profilePic' => $filename,
        );

        $this->db->where('userID',$userID)
        ->update('tblusers',$data);
    }

【讨论】:

  • 第 1 步应该粘贴到控制器的类中。
  • 还是不行。它甚至没有显示任何错误。至少 IF 块应该可以工作。我还添加了 $route['upload'] = 'Upload';在路由文件中。
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 2016-01-30
  • 2012-10-28
  • 1970-01-01
  • 2012-09-18
  • 2017-12-21
  • 2016-07-10
  • 2015-06-05
相关资源
最近更新 更多