CodeIgniter Forums

Full Version: Image Upload
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I try to upload an image:

The upload path does not appear to be valid.

Why does that error appears?


http://jamshidhashimi.com/2011/06/14/ima...igniter-2/


controllers/Site.php


PHP Code:
<?php

//Author: Jamshid HASHIMI
 
 
 class Upload extends CI_Controller
 
  
{
 
 
   function __construct()
 
 
   {
 
 
       parent::__construct();
 
 
       $this->load->helper('form');
 
 
       $this->load->helper('url');
 
 
    
 
    function index
()
 
 
   {
 
 
       $this->load->view('upload_view');
 
 
   }
 
 
   //Upload Image function
 
 
   function uploadImage()
 
 
   {
 
 
      $config['upload_path'    "uploads/";
 
 
      $config['allowed_types'] =   "gif|jpg|jpeg|png"
 
 
      $config['max_size'       "5000";
 
 
      $config['max_width'      "1907";
 
 
      $config['max_height'     "1280";
 
 
      $this->load->library('upload',$config);
 
 
      if(!$this->upload->do_upload())
 
 
      {
 
 
          echo $this->upload->display_errors();
 
 
      }
 
 
      else
 
 
      {
 
 
          $finfo=$this->upload->data();
 
 
          $this->_createThumbnail($finfo['file_name']);
 
 
          $data['uploadInfo'] = $finfo;
 
 
          $data['thumbnail_name'] = $finfo['raw_name']. '_thumb' .$finfo['file_ext']; 
 
 
          $this->load->view('upload_success',$data);
 
 
          // You can view content of the $finfo with the code block below
 
 
          /*echo '<pre>';
 
           print_r($finfo);
 
           echo '</pre>';*/
 
 
      }
 
 
   }
 
 
   //Create Thumbnail function
 
 
   function _createThumbnail($filename)
 
 
   {
 
 
       $config['image_library'   "gd2"     
 
        $config
['source_image'    "uploads/" .$filename     
 
        $config
['create_thumb'    TRUE     
 
        $config
['maintain_ratio'  TRUE     
 
        $config
['width'] = "80"     
 
        $config
['height'] = "80";
 
 
       $this->load->library('image_lib',$config);
 
 
       if(!$this->image_lib->resize())
 
 
       {
 
 
           echo $this->image_lib->display_errors();
 
 
            
 
    
}
 
 
 }
 
?>


views/upload_view.php


PHP Code:
<html>
 
<
head>
 
<
titleImage Upload </title>
 
</
head>
 
<
body>
 
<
div id="container">
 
<?
php echo  form_open_multipart('upload/uploadImage')?>
 
<input type="file" name="userfile" />
 
<p><input type="submit" name="submit" value="submit" /></p>
 
<?php echo form_close();?>
 
</div>
 
</body>
 
</html> 



views/upload_success.php


PHP Code:
<html>
 
<
head>
 
<
titleImage Upload </title>
 
</
head>
 
<
body>
 
<
div id="container">
 
<
dl>
 
 
   <dt>
 
 
       File Name:
 
 
   </dt>
 
 
   <dd>
 
 
       <?php echo $uploadInfo['file_name'];?>
 
    </dd>
 
    <dt>
 
        File Size:
 
    </dt>
 
    <dd>
 
        <?php echo $uploadInfo['file_size'];?>
 
    </dd>
 
    <dt>
 
        File Extension:
 
    </dt>
 
    <dd>
 
        <?php echo $uploadInfo['file_ext'];?>
 
    </dd>
 
    <br />
 
    <p>The Image:</p>
 
    <img alt="Your uploaded image" src="<?=base_url(). 'uploads/' $uploadInfo['file_name'];?>"> 
 
    <p>The Image:</p> 
 
    <img alt="Your Thumbnail image" src="<?=base_url(). 'uploads/' $thumbnail_name;?>">  
 
</dl>
 
</div>
 
</body>
 
</html> 
PHP is usually very good in it's error reporting. Here it is telling you that the path you are trying to upload your image to is invalid. The path you have used is "uploads/" but perhaps you meant to use "./uploads/" however it depends where you put the uploads folder in the first place.

Alternatively you can use an absolute server path something like "/sites/mydomain/public/uploads/" but the actual path will depend on your server set up.

This is well described in the documentation
http://www.codeigniter.com/user_guide/li...references

PS In general, reading the documentation, using php.net and google searches and working through these problems is a great way to learn, but you have to have an interest in learning why something has gone wrong and what the solution actually means, how it actually is working or solving an error. It is in solving these small problems that we learn about bigger issues, and become better programmers. For instance, these path problems can be solved with a bit of experimentation, moving the target folder, tracing where it is trying to look, testing to see if you can find it where it is looking, trying an absolute path, reading your hosts server path documentation, running a test script etc etc etc. After just an hour or two you will be thoroughly versed in the subject. Also, don't forget there are lots of great books on php available too, and working through a well written book is time well invested, as the writer will guide you from simple to more complex things in a controlled and thoughtful manner.

Hope that helps.