t0bi
t0bi

Reputation: 76

Codeigniter File Upload Internal Server Error

When I try to upload a file, I get a HTTP 500 Error. If someone could point me in the right direction. There are three files below, Upload.php, upload_success.php, and upload_form.php. I have properly setup the autoload, config, database files.

        <?php

       class Upload extends CI_Controller {

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

          public function index() { 
             $this->load->view('upload_form', array('error' => ' ' )); 
          } 

          public function do_upload() { 
             $config['upload_path']   = './uploads/'; 
             $config['allowed_types'] = '*'; 
             $config['max_size']      = 100; 
             $config['max_width']     = 1024; 
             $config['max_height']    = 768;  
             $this->load->library('upload', $config);

             if ( ! $this->upload->do_upload('userfile')) {
                $error = array('error' => $this->upload->display_errors()); 
                $this->load->view('upload_form', $error); 
             }

             else { 
                $data = array('upload_data' => $this->upload->data()); 
                $this->load->view('upload_success', $data); 
             } 
          } 
       } 
    ?>



<html>

   <head> 
      <title>Upload Form</title> 
   </head>

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

      <ul> 
         <?phpforeach ($upload_data as $item => $value):?> 
         <li><?php echo $item;?>: <?php echo $value;?></li> 
         <?phpendforeach; ?>
      </ul>  

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

</html>


        <html>

       <head> 
          <title>Upload Form</title> 
       </head>

       <body> 
          <?php echo $error;?> 
          <?php echo form_open_multipart('upload/do_upload');?> 

          <form action = "" method = "">
             <input type = "file" name = "userfile" size = "20" /> 
             <br /><br /> 
             <input type = "submit" value = "upload" /> 
          </form> 

       </body>

    </html>

Upvotes: 0

Views: 1857

Answers (2)

Vickel
Vickel

Reputation: 7997

on a shared hosting, you most likely need a different relative path than on a localhost environment:

you can use on a localhost environment

$config['upload_path']   = './uploads/'; 

but on a shared hosting, you'll need to supply more specific, something like

$config['upload_path']   = '/home/yourserver/public_html/uploads/'; 

you can find this in your accounts cPanel main page on the left column or call your providers helpdesk for more info on the correct path

Upvotes: 1

Denison Martins
Denison Martins

Reputation: 56

This code below seems a little odd. Not sure if could cause the 500 tho.

  <ul> 
     <?phpforeach ($upload_data as $item => $value):?> 
     <li><?php echo $item;?>: <?php echo $value;?></li> 
     <?phpendforeach; ?>
  </ul>  

Could you try to replace it with this instead?

  <ul> 
     <?php foreach ($upload_data as $item => $value): ?> 
     <li><?php echo $item;?>: <?php echo $value;?></li> 
     <?php endforeach; ?>
  </ul> 

Also, it is worth mentioning that in (what it seems to be) your last file you've opened two forms. One via form_open_multipart() and another one with pure html.

Please, remove this line:

<form action = "" method = "">

And replace this </form> with form_close() (the results will be the same, but just for code consistency).

Hope it helps.

Upvotes: 0

Related Questions