I am creating a gallery on my website and I have created several test accounts to make sure it works for each of them. But I only seem to be able to upload any image to the screen and gallery database for only one user, and not any other. Why is this - each user has their own session so I'm very confused. Please have a look at my code. My thoughts are that there is something wrong with my putGalleryImage function in my model but I don't see anything that really sticks out at me. Im thinking it could also be in the else statement of my upload function for when an upload of an image occurs but again, nothing sticks out at me because if it works for just one specific user, why not any other?
Controller:
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');
}
}
Model:
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;
}
}
View:
<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;?>
Thanks again for the help guys
It seems to be an OS problem related to directory access.