【问题标题】:Codeigniter uploaded file not move to folderCodeigniter 上传的文件没有移动到文件夹
【发布时间】:2017-11-29 09:31:56
【问题描述】:

我想在文件夹中上传文件,以下是我在文件夹中上传文件的代码,该代码在我的本地系统中成功运行,并且文件也移动到了文件夹中,但在服务器端,当我使用 @987654322 时,我得到了 'The upload path does not appear to be valid'. @;但是当我使用$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>

代码在我的本地系统中运行,但在服务器端文件未移动到文件夹。我也同意了。

【问题讨论】:

  • 在配置上传路径应该是 $config['upload_path'] = './assets/images/store/category/';并确保文件夹具有写入应用程序用户的权限
  • 我在我的问题中提到,当我使用 $config['upload_path'] = './assets/images/store/category/';我收到了成功消息,但上传的文件没有显示在文件夹中,我也获得了许可。
  • 你有两个表单标签在视图中
  • 我删除了表单,但仍然没有显示上传的文件。
  • @samirsheikh 再次您不会通过助手关闭表单标签。你的form_close() 呢??

标签: php codeigniter


【解决方案1】:

我可以看到有两个form 标签可以引发这个问题。所以修改一下

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

   <body> 
      <?php echo $error;?> 

      <form action ="<?php echo site_url('upload/Test_do_upload');?>" method="post" enctype="multipart/form-data">
         <input type = "file" name = "userfile" size = "20" /> 
         <br /><br /> 
         <input type = "submit" value = "upload" /> 
      </form> 

   </body>

</html>

【讨论】:

    【解决方案2】:

    如果没有创建目录,可以试试这个代码并尝试创建目录。否则打印 $error 变量以找出什么样的上传错误。可能这会有所帮助。并从您的重复视图中删除 form 标签

          $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*/
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 2013-12-26
      • 2013-07-27
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      相关资源
      最近更新 更多