[eluser]James_B[/eluser]
Hi guys
I've been following the file upload tutorial as closely as possible and trying to relate it to my own controller but when I try to upload an image it throws back the default error message instead of displaying it on the same screen.
Here is my code so far:
Homeprofile view:
<div id="maincontent">
<div id="primary">
<?$image;?>
<?=form_open_multipart('homeprofile/do_upload');?>
<input type="file" name="userfile" size="20" />
<?=form_submit('submit', 'homeprofile')?>
<?=form_close();?>
</div>
<div id="secondary">
<p>
<?=$profileText;?>
</p>
<p>
<?=form_open('homeprofile/changetext'); ?>
<?php $msgbox = array(
'name' => 'profiletext',
'rows' => '8',
'cols' => '30',
);?>
<?=form_textarea($msgbox);?>
</p>
<p>
<?=form_submit('submit', 'Change'); ?>
<?=form_close(); ?>
</p>
</div>
</div>
upload_form.php which throws the error back on the same view above:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
</body>
</html>
page which is meant to display image in homeprofile view:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $image => $value):?>
<li><?php echo $image;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
--------------------------
Controller:
<?php
class HomeProfile extends CI_Controller
{
function HomeProfile()
{
parent::__construct();
$this->load->model("profiles");
$this->load->model("profileimages");
$this->load->helper(array('form', 'url'));
}
function changetext()
{
$username = $this->session->userdata('username');
$this->profiles->putProfileText($username, $this->input->post("profiletext"));
redirect('homeprofile/index');
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//fail show upload form
if (! $this->upload->do_upload())
{
$error = array('error'=>$this->upload->display_errors());
$username = $this->session->userdata('username');
$viewData['username'] = $username;
$viewData['profileText'] = $this->profiles->getProfileText($username);
$this->load->view('shared/header');
$this->load->view('homeprofile/homeprofiletitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('homeprofile/upload_form', $error);
$this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
$this->load->view('shared/footer');
}
else
{
//successful upload so save to database
$data = array('upload_data' => $this->upload->data());
// you may want to delete the image from the server after saving it to db
// check to make sure $data['full_path'] is a valid path
$image = chunk_split( base64_encode( file_get_contents( $data['full_path'] ) ) );
$record = array('user' => $user, 'profileimage' => $image);
if ($this->_exists($user))
{
$this->db->update('profileimages', $record)->where('user', $user);
}
else
{
$this->db->insert('profileimages', $record);
}
// get upload_sucess.php from link above
$this->load->view('homeprofile/upload_success', $data);
}
}
function _exist($user){
$query = $this->db->select('*')->from('profileimages')->where('profileimage', $user)->limit(1);
return ($query->num_rows() > 0) ? true : false;
}
function displayImage()
{
//Retrieve image id from the URI
$user = $this->uri->segment(3);
$this->db->select('*')->from('profileimages')->where('user', $user);
$query = $this->db->get();
$image = null;
if ($query->num_rows() > 0){
$row = $query->row();
$image = base64_decode($row->profileimage);
}
if (!is_null($image)) {
//need to know the mine type
// header('Content-Type: image/png');
header ('Content-Type: image/jpg');
imagejpeg(imagecreatefromstring($image), null,100);
}
else{
// load a default profile image
}
}
function viewProfile(){
//load profile detail view for users and display image
// 18 = change to image id
echo '<img src="<?=base_url()?>profile/displayImage/18" alt="profile"/>';
}
function index()
{
$username = $this->session->userdata('username');
$viewData['username'] = $username;
$viewData['profileText'] = $this->profiles->getProfileText($username);
$this->load->view('shared/header');
$this->load->view('homeprofile/homeprofiletitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
$this->load->view('shared/footer');
}
}
------------------------------------------------------
Model:
<?php
class ProfileImages extends CI_Model
{
function ProfileImages()
{
parent::__construct();
}
function putProfileImage($user, $image)
{
$record = array('user' => $user, 'profileimage' => $image);
if ($this->exists($user))
{
$this->db->update('profileimages', $record)->where('user', $user);
}
else
{
$this->db->insert('profileimages', $record);
}
}
function getProfileImage($user)
{
$this->db->select('*')->from('profileimages')->where('user', $user);
$query = $this->db->get();
if ($query->num_rows() > 0){
$row = $query->row();
return $row->profileimage;
}
return Null;
}
}