EllisLab text mark
Advanced Search
     
Why can’t I get an image to upload?
Posted: 11 November 2012 09:20 AM
Joined: 2012-11-04
22 posts

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?

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

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

upload fail:

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

upload success:

<
html>
<
head>
<
title>Upload Form</title>
</
head>
<
body>

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

<
img src="<?php echo $img?>width="300" height="300"/>
 
<
p><?php echo anchor('upload''Upload Another File!'); ?></p>

</
body>
</
html
 
Posted: 11 November 2012 10:17 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-11
173 posts
$config['upload_path''/images/'

change to

$config['upload_path''./images/'
 
Posted: 12 November 2012 06:06 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-04
22 posts

now saving to folder - thanks