Welcome Guest, Not a member yet? Register   Sign In
Image file upload issue
#1

[eluser]James_B[/eluser]
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:

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


Controller:
Code:
&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 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');
    
   }
  
  }
#2

[eluser]jmadsen[/eluser]
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
#3

[eluser]xeroblast[/eluser]
Code:
&lt;?php echo $error;?&gt;
change to
Code:
&lt;?php if ( isset($error) ) echo $error;?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB