Codeigniter文件上载内部服务器错误

时间:2016-12-13 20:12:02

标签: php codeigniter

当我尝试上传文件时,出现HTTP 500错误。如果有人能指出我正确的方向。下面有三个文件,Upload.php,upload_success.php和upload_form.php。我已正确设置自动加载,配置,数据库文件。

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

2 个答案:

答案 0 :(得分:1)

在共享主机上,您很可能需要一个与本地主机环境不同的相对路径:

您可以在localhost环境中使用

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

但是在共享主机上,您需要提供更具体的内容,例如

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

您可以在左侧列的帐户cPanel主页中找到此信息,或致电您的提供商帮助台以获取有关正确路径的更多信息

答案 1 :(得分:0)

下面这段代码看起来有点奇怪。不确定是否会导致500 tho。

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

您可以尝试用它替换吗?

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

另外,值得一提的是,在您的上一个文件(似乎是)中,您打开了两个表单。一个通过form_open_multipart(),另一个通过纯HTML。

请删除此行:

<form action = "" method = "">

并将此</form>替换为form_close()(结果将相同,但仅用于代码一致性)。

希望它有所帮助。