Codeigniter上传的文件不会移动到文件夹

时间:2017-11-29 09:31:56

标签: php codeigniter

我想在文件夹中上传文件,以下是我在文件夹中上传文件的代码,代码在本地系统中成功运行,文件也移动到文件夹但在服务器端我使用时'The upload path does not appear to be valid'. $config['upload_path'] = './uploads/';但是当我使用$config['upload_path'] = './assets/images/store/category';时,我收到了成功消息,但上传的文件没有显示在category文件夹中。

以下是我的代码。

控制器

<?php  
   class Upload extends CI_Controller {

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

      public function index() { 
         echo "Tst";
         $this->load->view('Upload_form', array('error' => ' ' )); 
      } 

      public function Test_do_upload() { 
         $config['upload_path']   = './uploads/'; 
         $config['allowed_types'] = 'gif|jpg|png'; 
         $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()); 
            echo '<pre>';
            print_r($data);
          //  $this->load->view('upload_success', $data); 
         } 
      } 
   } 
?>

查看(表格)

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

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

         <input type = "file" name = "userfile" size = "20" /> 
         <br /><br /> 
         <input type = "submit" value = "upload" /> 
         <?= form_close(); ?>       
   </body>

</html>

成功上传视图

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

代码在我的本地系统中工作,但在服务器端文件中没有移动到文件夹。我也同意了。

1 个答案:

答案 0 :(得分:0)

可能会尝试此代码并尝试创建目录(如果未创建)。否则打印 $ error 变量以找出哪种上传错误。可能这会有所帮助。并从您的视图中删除表单标记重复。

      $dir_path= 'assets/admin/uploads/course_images/';
      if (!is_dir($dir_path)) {
            mkdir($dir_path, 0777, TRUE);
        }
        $new_name = time().$this->input->post('Course_code');
        //move_uploaded_file($_FILES['file']['tmp_name'], $dir_path);
        $config['upload_path'] = $dir_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = FALSE;
        $config['max_size'] = '1000';
        $config['file_name'] = $new_name;
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload('file'))
        {
            // no file uploaded or failed upload
            $error = array('error' => $this->upload->display_errors());
            /*Print Error*/
        }else{
            /*Insert Code*/
        }