我是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" />
但它沒有幫助。誰能幫我?任何建議和建議都非常歡迎。謝謝。
是的,它也是這樣工作的。 –