0

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>
5
  • 1
    are you using localhost server or a production server (like a shared hosting server)? Commented Dec 13, 2016 at 20:22
  • maybe a permissions problem? Commented Dec 13, 2016 at 20:44
  • Might be be your htaccess file and folder permissions 0777 for uploads folder also make sure you have set your config base url don't leave it blank in config.php Commented Dec 13, 2016 at 20:56
  • Sorry did not see these comments. I am using a shared hosting server(godaddy). In the file manager, I granted permissions to the "uploads" folder which is located at the root and I have set my base url. Can anyone direct me to a .htaccess file that I should use? Commented Dec 13, 2016 at 23:47
  • did u fix the error? Commented Jan 5, 2017 at 9:39

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

I changed the path to G:\\PleskVhosts\\etc and I am still getting the 500 error. I am fairly certain this path format is correct, as my php successfully writes to my sessions folder. In config/database.php where $db['default'] = array, what should the 'hostname' be? I am not sure if this would be part of my problem...
that most likely is "localhost". don't forget, once you add a new database at the shared server, you'll need to add a user as well, otherwise this doesn't work. Also the path you mention in your comment is not relative but a windows like path
@user7292978, if you're on a GoDaddy shared hosting account, then G:\\PleskVhosts\\etc makes absolutely no sense to me. Try the path as instructed in the 'shared hosting' section of this answer above!
The user is unique, and I changed the path to: /PleskVhosts/domain.com/sub.domain.com/uploads without path errors but still getting 500 error. I used a Firefox addon to inspect the page events and the _POST method fails to write to the database.
the function do_upload() is not writing to any database at all, it is uploading a file to some directory on your server. do check folder permissions. also check if you are actually uploading now, but not writing to the database (a function you didn't show us yet)
|
0

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.

2 Comments

Thank you for the response, I made the changes you suggested and I am still receiving a 500 error. When I click upload after selecting a file, the page loads and fails on /upload/do_upload/ and gives a 500 error. Any other thoughts?
besides of the misspelled php code, this is not going to provoke a 500 error, w. this error means you sent some info to the server which it cannot deal with, hence the 500 error. That's why I asked earlier, are you on localhost or production?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.