• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Uploading File

#1
Hello,

I am trying to upload some file but have not been successful yet doing this.

This my upload CI codes.  My target extension is JPG/GIF


controllers/uploadfile.php


PHP Code:
<?php



class uploadfile extends CI_Controller {

 
   function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->helper(array('form''url'));
 
   }

 
   //index function
 
   function index()
 
   {
 
       //load file upload form
 
       $this->load->view('upload_file_view');
 
   }

 
   //file upload function
 
   function upload()
 
   {
 
       //set preferences
 
       $config['upload_path'] = 'uploads/';
 
       $config['allowed_types'] = 'txt|pdf';
 
       $config['max_size'   '100';

 
       //load upload class library
 
       $this->load->library('upload'$config);

 
       if (!$this->upload->do_upload('filename'))
 
       {
 
           // case - failure
 
           $upload_error = array('error' => $this->upload->display_errors());
 
           $this->load->view('upload_file_view'$upload_error);
 
       }
 
       else
        
{
 
           // case - success
 
           $upload_data $this->upload->data();
 
           $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
 
           $this->load->view('upload_file_view'$data);
 
       }
 
   }
}


?>



views/upload_file_view.php


PHP Code:
<!DOCTYPE html>
<
html>
<
head>
 
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
 
   <title>CodeIgniter File Upload Form</title>
 
   <!-- load bootstrap css file -->
 
   <link href="<?php echo base_url("assets/bootstrap/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</
head>

<
body>
<
div class="container">
 
   <div class="row">
 
       <div class="col-md-6 col-md-offset-3 well">
 
       <legend>CodeIgniter File Upload Demo</legend>
 
       <?php echo form_open_multipart('uploadfile/upload');?>

        <fieldset>
            <div class="form-group">
                <div class="row">
                    <div class="col-md-12">
                        <label for="filename" class="control-label">Select File to Upload</label>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="row">
                    <div class="col-md-12">
                        <input type="file" name="filename" size="20" />
                        <span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="row">
                    <div class="col-md-12">
                        <input type="submit" value="Upload File" class="btn btn-primary"/>
                    </div>
                </div>
            </div>
        </fieldset>
        
        <?php echo form_close(); ?>
        <?php if (isset($success_msg)) { echo $success_msg; } ?>
        </div>
    </div>
</div>
</body>

</html> 


This is the view:


CodeIgniter File Upload Demo

Select File to Upload

[ Browse ]
 
The upload path does not appear to be valid.

[ Upload File ]


When I try to upload JPG/GIF nothing happens, it only loads nothing.  With this error message:

The upload path does not appear to be valid.

----------------------------------

controllers/uploadfile.php

PHP Code:
function upload()
 
   {
 
       //set preferences
 
       $config['upload_path'] = 'uploads/'


controllers/uploads/  

(uploads folder address)

How to fix the error?
" If I looks more intelligence please increase my reputation."
Reply

#2
If you use a relative upload file path, as you have, it is relative to the location of your index.php front controller.
That means that the library will be looking for PROJECT/uploads with your configuration, and *not* for PROJECT/application/controllers/uploads.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.