EllisLab text mark
Advanced Search
     
Image file upload issue
Posted: 10 November 2012 02:49 PM   [ Ignore ]
Joined: 2012-11-04
22 posts

Ok guys this is really gettin on my nerves this issue - How come when I go on to my profile page which features a profile image upload function when I first access the page I first get the error:

Severity: Notice

Message: Undefined variable: error

Filename: homeprofile/upload_form.php

Line Number: 3

This error is Im supposed to see should only appear when I dont an image to upload despite pressing upload - You did not select a file to upload.

And then when I DO select a file to upload I get this error instead of the success page - The upload path does not appear to be valid. I have made sure the images folder was easily accessible

Please please take a look at my code:

View file

Upload_form:

<div id="maincontent">
      <
div id="primary"
      
<?php echo $error;?>
        <?
=form_open_multipart('homeprofile/upload');?>
        
<input type="file" name="userfile" />
        
<?=form_submit('submit''upload')?>
        <?
=form_close();?> 
      
</div
</
div


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 upload()
  
{
   $config[
'upload_path''/imges/';
   
$config['allowed_types''gif|jpg|png';
   
$config['max_size''10000';
   
$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
    
    
$file_data $this->upload->data();
    
$data['img'base_url().'/images/'.$file_data['file_name'];
    
// 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);
    

    
$username $this->session->userdata('username');
     
   
}
   
  } 
 
Posted: 10 November 2012 05:53 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-04-15
454 posts

nothing to do with the upload code.

you are only defining the variable in one part of your conditional, but the view is trying to output it even when there is no error. $error doesn’t exist, so error message

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 11 November 2012 11:19 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-11
167 posts
<?php echo $error;?> 

change to

<?php if ( isset($error) ) echo $error;?>