CodeIgniter Forums
Why can't I get an image to upload? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Why can't I get an image to upload? (/thread-55816.html)



Why can't I get an image to upload? - El Forum - 11-11-2012

[eluser]James_B[/eluser]
Hi guys I have been following a tutorial online to upload an image to a page but when I upload something I get an error message: The file path does not seem to be valid. And I also get this error instead of seeing you did not select a file to upload error when I select upload without actually selecting any files

What I'm aiming to do is simply display the image to the screen once its uploaded

Ive tried creating an images folder in my root folder where the system folder is, but to no avail

What am i doing wrong?

Code:
<?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'] = '/images/';
   $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_fail', $error);
    $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
    $this->load->view('shared/footer');
  
   }
  
   else
   {

    
    $file_data = $this->upload->data();
    $data['img'] = base_url().'/images/'.$file_data['file_name'];
    
    $this->load->view('upload_success', $data);
    

    $username = $this->session->userdata('username');
    
   }
  
  }
    
  
  
  
  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/upload_form' );
   $this->load->view('homeprofile/homeprofileview', $viewData );
   $this->load->view('shared/footer');
  }

}

Here are my view files:

upload form:

This file present the form onto the screen

Code:
<div id="maincontent">
   <div id="primary">
      &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>

upload fail:

<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>

upload success:

&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>

<img src="&lt;?php echo $img?&gt;" width="300" height="300"/>

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

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



Why can't I get an image to upload? - El Forum - 11-11-2012

[eluser]xeroblast[/eluser]
Code:
$config['upload_path'] = '/images/';
change to
Code:
$config['upload_path'] = './images/';



Why can't I get an image to upload? - El Forum - 11-12-2012

[eluser]James_B[/eluser]
now saving to folder - thanks