在Codeigniter中上传和检索视图路径的图像路径

时间:2017-04-03 08:43:43

标签: php codeigniter

我是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" />

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

2 个答案:

答案 0 :(得分:4)

试试这个

Sub DeleteRows()
    ' 03 Apr 2017

    Dim Cell As Range
    Dim InputRng As Range
    Dim DeleteRng As Range
    Dim DeleteStr As String
    Dim xTitleId As String

    xTitleId = "Delete"
    Set InputRng = Application.InputBox("Range :", xTitleId, Selection.Address, Type:=8)
    DeleteStr = InputBox("Delete Text", xTitleId)
    For Each Cell In InputRng
        If StrComp(Cell.Value, DeleteStr, vbTextCompare) Then
            If DeleteRng Is Nothing Then
                Set DeleteRng = Cell.EntireRow
            Else
                Set DeleteRng = Application.Union(DeleteRng, Cell.EntireRow)
            End If
        End If
    Next
    DeleteRng.Delete
End Sub

还可以核心设置文件夹位置

答案 1 :(得分:0)

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