Welcome Guest, Not a member yet? Register   Sign In
Trying to upload a profile picture to a page
#1

[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">
&lt;?$image;?&gt;
&lt;?=form_open_multipart('homeprofile/do_upload');?&gt;
&lt;input type="file" name="userfile" size="20" /&gt;
&lt;?=form_submit('submit', 'homeprofile')?&gt;
&lt;?=form_close();?&gt;
</div>
<div id="secondary">
<p>
&lt;?=$profileText;?&gt;
</p>
<p>
&lt;?=form_open('homeprofile/changetext'); ?&gt;
&lt;?php $msgbox = array(
'name' => 'profiletext',
'rows' => '8',
'cols' => '30',
);?&gt;
&lt;?=form_textarea($msgbox);?&gt;
</p>
<p>
&lt;?=form_submit('submit', 'Change'); ?&gt;
&lt;?=form_close(); ?&gt;
</p>
</div>
</div>

upload_form.php which throws the error back on the same view above:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo $error;?&gt;



&lt;/body&gt;
&lt;/html&gt;

page which is meant to display image in homeprofile view:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your file was successfully uploaded!</h3>

<ul>
&lt;?php foreach ($upload_data as $image => $value):?&gt;
<li>&lt;?php echo $image;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

<p>&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;
--------------------------
Controller:

&lt;?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="&lt;?=base_url()?&gt;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:
&lt;?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;


}

}

#2

[eluser]James_B[/eluser]
Ok, Im thinking I should probably do this step by step, so I want to be able to first upload an image into the uploads folder that I have, before even thinking of it trying to display on screen

Im thinking where I'm struggling is the do upload function:

function do_upload()
{
$config['upload_path'] = './/assets/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
// get upload_sucess.php from link above
$this->load->view('upload_success', $data);
$image = chunk_split( base64_encode( file_get_contents( $data['profileimages'] ) ) );



}

}

What here needs to be amended to the else part of this function?




Theme © iAndrew 2016 - Forum software by © MyBB