2017-04-03 3 views
1

私はcodeigniterを新しくしました。そして、私は製品の画像をアップロードしようとしていました。今、私は各製品のために1つのイメージだけをアップロードしようとしています。イメージパスを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" /> 

が、それは助けていない:私はまた、このように手動で画像のルートフォルダを与えてみました。誰でも助けてくれますか?どんな提案や助言も大歓迎です。ありがとうございます。

答えて

4

この

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

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

を試みるもcorectly

+0

これはあまりにもこの方法で働いた。 –

0

だけ愚かな過ちを自分のフォルダの場所を設定します。私はバーチャルホストを作成して、ルートフォルダのindex.htmlを指すようにしました。ルートフォルダを指すだけで問題は解決しました。

関連する問題