图像已保存但未返回(无响应)

时间:2018-01-31 09:51:57

标签: php codeigniter

我正在尝试在保存后从数据库中获取数据和图像。数据和图像保存正确,但是当我尝试读取它们时,会发生错误。我无法解决问题。

我的观点是:

view1 Upload_form.php

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

view2 Upload_success.php

<html>
  <head> 
     <title>Upload Form</title> 
  </head>
  <body>  
     <h3>Your file was successfully uploaded!</h3>  
     <?php foreach ($image as $item) { ?>
        <P> <?php echo $item['title']; ?> </P>
        <img src="<?php echo base_url().'uploads/'. $item['image']; ?>">
     <?php } ?>
     <ul> 
        <?php foreach ($upload_data as $item => $value) { ?> 
        <li><?php echo $item;?>: <?php echo $value;?></li> 
        <?php } ?>
     </ul>  
     <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>  
  </body>
</html>

我认为错误发生在else声明中。请检查一下。

控制器的名称为upload.php

  <?php

     class Upload extends CI_Controller 
     {

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

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

        public 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('userfile')) 
           {
              $error = array('error' => $this->upload->display_errors()); 
              $this->load->view('upload_form', $error); 
           }

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

模型的名称为model.php

<?php
    class model extends CI_Model
    {
        function __construct()
        {
            parent::__construct();
            $this->load->database();
        }
        public function insertdata()
        {

                $upload_data = $this->upload->data();
                $data = array(
                    'name' => $this->input->post('title'),
                    'image' => $upload_data['file_name'],
                );
                $this->db->insert('image', $data);
                $id= $this->db->insert_id();

                $this->db->select('*');
                $this->db->from('image');
                $this->db->where('id', $id);
                $d = $this->db->get();
                return $d->result();
        }
    }

3 个答案:

答案 0 :(得分:0)

这是我上传和显示图片的方式。希望它对你最有帮助。 在您的控制器中:

            if (count($_FILES) > 0) 
            {
                // upload Profile Picture
                $config['upload_path']   = './uploads/profile_pic/';
                $config['allowed_types'] = 'jpg|jpeg|png|JPG|JPEG|PNG';

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

                $this->upload->initialize($config);

                $is_profile_pic = $this->upload->do_upload('profilePic');

                if ($is_profile_pic) 
                {
                    $ret    = $this->upload->data();
                    $pic    = $ret['file_name'];
                }
                else
                {
                    $pic = "";
                }
            }

                $data = array(
                        'profile_pic'       => $pic 
                 );

                $this->User_Model->insert_into_users($data);

从数据库表中获取文件的名称,然后从uploads文件夹中选择此文件,在您的视图中显示如下:

  <img class="img-responsive avatar-view" src="<?php echo base_url() . 'uploads/profile_pic/'.$employee->profile_pic; ?>" alt="Avatar">

在我的项目中,它非常简单,完美。你正在努力尝试。

答案 1 :(得分:0)

Controller中的else应该是这样的:

else 
{  
    $data['image'] = $this->m->insertdata();
    $data['upload_data'] = $this->upload->data(); 
    $this->load->view('upload_success',  $data); 
}

答案 2 :(得分:0)

view2 Upload_success.php)中,您尝试使用以下内容显示图片:

<?php foreach ($image as $item) { ?>
    <P> <?php echo $item['title']; ?> </P>
    <img src="<?php echo base_url().'uploads/'. $item['image']; ?>">
<?php } ?>

这不起作用,因为您调用了$image变量,该变量未在您的控制器中定义。您使用该代码加载视图:

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

您可以在视图中访问的唯一变量是$upload_data。如果您在成功视图中执行<?php var_dump($upload_data); ?>,则会在使用这些键的数组中找到上传结果:

Array
(
    [file_name]     => mypic.jpg
    [file_type]     => image/jpeg
    [file_path]     => /path/to/your/upload/
    [full_path]     => /path/to/your/upload/jpg.jpg // You need this one!
    [raw_name]      => mypic
    [orig_name]     => mypic.jpg
    [client_name]   => mypic.jpg
    [file_ext]      => .jpg
    [file_size]     => 22.2
    [is_image]      => 1
    [image_width]   => 800
    [image_height]  => 600
    [image_type]    => jpeg
    [image_size_str] => width="800" height="200"
)

在您的视图中,您已经使用它来显示键/值:

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

解决方案:您只需获取full_path键并显示您的图片,如下所示:

<html>
  <head> 
     <title>Upload Form</title> 
  </head>
  <body>  
     <h3>Your file was successfully uploaded!</h3>  
     <img src="<?php echo base_url() . $upload_data['full_path']; ?>" alt="" />
     <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>  
  </body>
</html>

更多详情:Documentation