我正在我的网站上创建一个画廊,并且我创建了几个测试帐户以确保它适用于每个帐户。但我似乎只能为一个用户上传任何图像到屏幕和图库数据库,而不是其他任何用户。为什么会这样 - 每个用户都有自己的会话,所以我很困惑。请看一下我的代码。我的想法是我的模型中的 putGalleryImage 函数有问题,但我没有看到任何真正突出的东西。我认为它也可能出现在我的上传功能的 else 语句中,用于上传图像时,但同样,我没有任何问题,因为如果它只适用于一个特定用户,为什么不适用于其他任何用户?
控制器:
class Gallery extends CI_Controller {
function __construct()
{
// Call the parent construct
parent::__construct();
$this->load->model("profiles");
$this->load->model("gal_model");
$this->load->helper(array('form', 'url'));
$this->gallery_path = 'web-project-jb/assets/gallery/';
}
function upload()
{
$config = array(
'allowed_types' =>'gif|jpg|jpeg|png',
'upload_path' => $this->gallery_path,
'max_size' => 10000,
'max_width' => 1024,
'max_height' => 768);
$this->load->library('upload', $config);
$username = $this->session->userdata('username');
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$username = $this->session->userdata('username');
$viewData['username'] = $username;
$viewData['images'] = $this->gal_model->getGalleryImage($username);
$this->load->view('shared/header');
$this->load->view('gallery/galtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('gallery/galview', $error, $viewData, array('error' => ' ' ));
$this->load->view('shared/footer');
}
else
{
$file_data = $this->upload->data();
$galleryImage = $this->gallery_path.$file_data['file_name'];
$data['galleryImage'] = $this->gallery_path.$file_data['file_name'];
$this->username = $this->session->userdata('username');
$images = $this->session->userdata('images');
$data['images'] = $images;
$this->gal_model->putGalleryImage($username, $galleryImage);
$viewData['username'] = $username;
$viewData['images'] = $this->gal_model->getGalleryImage($username);
var_dump($galleryImage);
$username = $this->session->userdata('username');
$this->load->view('shared/header');
$this->load->view('gallery/galtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('gallery/galview', $viewData);
$this->load->view('shared/footer');
}
}
function index()
{
$username = $this->session->userdata('username');
$this->load->library('upload');
$data['gal_model'] = $this->gal_model->getGalleryImage($username);
$viewData['username'] = $username;
$viewData['images'] = $this->gal_model->getGalleryImage($username);
$this->load->view('shared/header');
$this->load->view('gallery/galtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('gallery/galview', $viewData, array('error' => ' ' ));
$this->load->view('shared/footer');
}
}
模型:
class Gal_model extends CI_Model
{
var $gallery_path;
function Gal_model()
{
parent::__construct();
}
function exists($username)
{
$this->db->select('*')->from("gallery")->where('user', $username);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return true;
/*
echo "user $user exists!";
$row = $query->row();
echo " and his profileimage is $row->profileimage";
*/
}
else
{
return false;
//echo "no such user as $user!";
}
}
function putGalleryImage($username, $galleryImage)
{
$record = array('user' => $username, 'galleryimage' => $galleryImage);
$this->session->set_userdata($galleryImage);
if ($this->exists($username))
{
$this->db->where('user', $username)->insert('gallery', $record);
}
}
function getGalleryImage($username)
{
$this->db->select('*')->from('gallery')->where('user', $username);
$imagesSet= $this->db->get();
$images = array();
foreach ($imagesSet->result() as $row)
{
$images[] = array('imageid' => $row->imageid,
'user' => $row->user,
'galleryimage' => $row->galleryimage);
}
return $images;
}
}
看法:
<div id="gallery">
<?php if (isset($images) && is_array($images)):
foreach($images as $galleryImage):
$link = $galleryImage['galleryimage'] ?>
<div class="thumb">
<img src="<?php echo base_url().$link; ?>" width='150' height='100'/>
<br>
</div>
<?php endforeach; else: ?>
<div id = "blank_gallery">Please upload an Image</div>
<?php endif; ?>
<?=form_open_multipart('gallery/upload');?>
<?=form_upload("userfile");?>
<?=form_submit('upload', 'Upload')?>
<?=form_close();?>
<?php if (isset($error)) echo $error;?>
再次感谢大家的帮助