【问题标题】:Image path uploading and retrieving it in view in Codeigniter在 Codeigniter 的视图中上传和检索图像路径
【发布时间】:2017-04-03 08:43:42
【问题描述】:

我是 codeigniter 的新手。我正在尝试为产品上传图片。现在我正在尝试为每个产品只上传一张图片。我已经在我的控制器中做到了这一点:

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
    class Users extends CI_Controller
    {
    function  __construct() {
    parent::__construct();
    $this->load->model('user');
    }

     function add(){
      if($this->input->post('userSubmit')){

        //Check whether user upload picture
        if(!empty($_FILES['picture']['name'])){
            $config['upload_path'] = 'uploads/images/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['file_name'] = $_FILES['picture']['name'];

            //Load upload library and initialize configuration
            $this->load->library('upload',$config);
            $this->upload->initialize($config);

            if($this->upload->do_upload('picture')){
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
            }else{
                $picture = '';
            }
        }else{
            $picture = '';
        }

        //Prepare array of user data
        $userData = array(
            'name' => $this->input->post('name'),
            'email' => $this->input->post('email'),
            'picture' => $picture
        );

        //Pass user data to model
        $insertUserData = $this->user->insert($userData);

        //Storing insertion status message.
        if($insertUserData){
            $this->session->set_flashdata('success_msg', 'User data have been added successfully.');
        }else{
            $this->session->set_flashdata('error_msg', 'Some problems occured, please try again.');
        }
    }
    //Form for adding user data
    $data['data']=$this->user->getdata();
    $this->load->view('show',$data);
}

public function show()
{
    #code
    $data['data']=$this->user->getdata();
    $this->load->view('show',$data);

}

}

我的模型中有这个:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class User extends CI_Model{
   function __construct() {
    $this->load->database();
    $this->tableName = 'users';
    $this->primaryKey = 'id';
}

   public function insert($data = array()){
    if(!array_key_exists("created",$data)){
        $data['created'] = date("Y-m-d H:i:s");
    }
    if(!array_key_exists("modified",$data)){
        $data['modified'] = date("Y-m-d H:i:s");
    }
    $insert = $this->db->insert($this->tableName,$data);
    if($insert){
        return $this->db->insert_id();
    }else{
        return false;
    }
}

    public function getdata()
{
  $query=$this->db->get('users');
  return $query->result_array();
}

}

问题是我能够将数据与图像名称一起存储在数据库中,并且所选图像已上传到项目文件夹中的指定文件夹中,当前为:

    root folder->image:
               -application
               -system
               -uploads
                  .images(images are saved here)

现在的问题在于视图。我正在尝试像这样动态访问存储的图像:

      <!DOCTYPE html>
       <html>
     <head>
   <meta charset="utf-8">
   <title>show</title>
    </head>
       <body>


    <table border='1' cellpadding='4'>
        <tr>
            <td><strong>User_Id</strong></td>
            <td><strong>Name</strong></td>
            <td><strong>Image</strong></td>
            <td><strong>Option</strong></td>
        </tr>
    <?php foreach ($data as $p): ?>
            <tr>
                <td><?php echo $p['id']; ?></td>
                <td><?php echo $p['name']; ?></td>
                <td><img src="../uploads/images/<?php echo $p['picture']; ?>" /> </td>
                <td>
                    <a href="#">View</a> |

                </td>
            </tr>
    <?php endforeach; ?>
    </table>

使用此图像源,图像不会出现。只看到图像裂纹缩略图。我也尝试像这样手动提供图像的根文件夹:

    <img src="../uploads/images/image-0-02-03-15420e726f6e9c97408cbb86b222586f7efe3b002e588ae6bdcfc3bc1ea1561e-V.jpg" />
     or like this
     <img src="../../uploadsimages/image-0-02-03-15420e726f6e9c97408cbb86b222586f7efe3b002e588ae6bdcfc3bc1ea1561e-V.jpg" />

但它没有帮助。谁能帮我?非常欢迎任何建议和建议。谢谢。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    试试这个

         <img src="<?php echo base_url('uploads/images/'.$p['picture']); ?>"/>
    
    insted of this line 
    <img src="../uploads/images/<?php echo $p['picture']; ?>" />
    

    还可以正确设置您的文件夹位置

    【讨论】:

      【解决方案2】:

      只是一个愚蠢的错误。我让我的虚拟主机指向根文件夹的 index.html。所以只要指向根文件夹问题就解决了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 2015-06-13
        • 2017-07-18
        • 1970-01-01
        • 1970-01-01
        • 2018-10-27
        • 1970-01-01
        相关资源
        最近更新 更多