2017-10-20 331 views
-1

我已经创建了根目录并设置了其路径,我完成了前端。 ide没有给出任何错误,但是当我继续时它说你没有选择任何图像。上传文件在codeigniter中不工作3错误:您没有选择任何文件

控制器代码:

class Images extends MY_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 
    } 

    function index() 
    { 
     $this->data['displayname'] = $this->admin->lastname; 
     $this->data['images'] = Image::all(); 
     $this->data['btn'] = 'Save'; 
     $this->data['content'] = 'admin/images/index'; 
     $this->load->view('layouts/admin', $this->data); 

    } 
    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('images')) 
     { 
      $error = array('error' => $this->upload->display_errors()); 
      // echo $this->upload->display_errors(); 
      $this->load->view('index', $error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 

      $this->load->view('upload_success', $data); 
     } 

    } 
} 

视图代码:这个是选择文件

<?= form_open('admin/images/do_upload') ?> 
      <label><?= form_upload('images[]')?></label> 
      <div class="input-group-btn"> 
       <button class="btn btn-success add-image" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button> 
      </div> 
     </div> 

    </ul> 

    <div class="line"></div> 

<?= form_submit('', 'Submit', 'class="publish-btn"') ?> 

<?= form_close()?> 
+0

笨上传库同时 – user4419336

回答

0

以及经过大量的研究和尝试大量的代码,我解决我的问题 这里是完整的东西 控制器代码:

public function create($id) 
    { 
     $config ['upload_path'] = 'uploads'; 
     $config ['allowed_types'] = 'gif|jpg|png'; 
     $config ['encrypt_name'] = TRUE; 

     $this->upload->initialize($config); 
     $package = Package::find($id); 

     $files = $_FILES; 
     $cpt = count($_FILES ['images'] ['name']); 

     for ($i = 0; $i < $cpt; $i++) { 

      $_FILES ['images'] ['name'] = $files ['images'] ['name'] [$i]; 
      $_FILES ['images'] ['type'] = $files ['images'] ['type'] [$i]; 
      $_FILES ['images'] ['tmp_name'] = $files ['images'] ['tmp_name'] [$i]; 
      $_FILES ['images'] ['error'] = $files ['images'] ['error'] [$i]; 
      $_FILES ['images'] ['size'] = $files ['images'] ['size'] [$i]; 
      if ($this->upload->do_upload('images')){ 
       $upload_data = $this->upload->data(); 
       $file_name = $upload_data['file_name']; 
       $package->create_images(array('image_location' => $file_name)); 
      } 
     } 

     redirect('admin/packages/'); 
    } 
} 

视图代码:

<div class="container"> 
    <?= form_open_multipart('admin/images/create/' . $this->uri->segment(4)) ?> 
    <div class="row"> 
     <div class="col-md-8 publish"> 
      <h4>Image Gallery</h4> 
      <div class="line"></div> 
      <ul> 
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> 



        <div class="input-group control-group after-add-image"> 


         <label><input type="file" name="images[]"></label> 

         <div class="input-group-btn"> 
          <button class="btn btn-success add-image" type="button"><i class="glyphicon glyphicon-plus"></i> Add</button> 
         </div> 
        </div> 

       </ul> 

       <div class="line"></div> 

      <?= form_submit('', 'Submit', 'class="publish-btn"') ?> 

      <?= form_close()?> 
      <div class="clear-all"></div> 
      <!-- Copy Fields-These are the fields which we get through jquery and then add after the above input,--> 
      <div class="copy-field" style="display: none"> 
       <div class="control-group input-group" style="margin-top:10px"> 
        <label><label><input type="file" name="images[]"></label></label> 
        <div class="input-group-btn"> 
         <button class="btn btn-danger delete" type="button"><i class="glyphicon glyphicon-delete"></i> Remove</button> 
        </div> 
       </div> 
      </div> 
      <script type="text/javascript"> 
       $(document).ready(function() { 

        //here first get the contents of the div with name class copy-fields and add it to after "after-add-more" div class. 
        $(".add-image").click(function(){ 
         var html = $(".copy-field").html(); 
         $(".after-add-image").after(html); 
        }); 
//here it will remove the current value of the remove button which has been pressed 
        $("body").on("click",".delete",function(){ 
         $(this).parents(".control-group").remove(); 
        }); 

       }); 
      </script> 

其获得所有图像动态地上传到目录,然后将路径存储到数据库 完成!

the image for front end

0

在文件上传时的形式,你应该使用

<?php form_open_multipart('admin/images/do_upload') ?> 

不是这

<?php form_open('admin/images/do_upload') ?> 
+0

它不上传图片 –

+0

是建立一个上传说上传路径不似乎是有效 –

+0

问题解决了要上传的文件。问题出在视图文件而不是控制器。因为我用这种形式没有响应的函数: 文件,因为我要去提交多个图像的阵列。 它在我使用时有效: 单个文件。 但我需要同时提交多个图像。我应该怎么做才能解决这个问题,因为我已经尝试过使用数组机制,而且它不工作。 –

1
<?= form_open_multipart('admin/images/do_upload') ?> 

使用上面的代码,而不是

<?= form_open('admin/images/do_upload') ?> 
0

我加入了多到我的形式加入

$this->upload->initialize($config); 

后制成的。我通过初始化库解决无效路径的问题在这行加载库之后。

$this->load->library('upload', $config); 

首先加载库然后初始化。 但仍然没有解决选择的问题 我也尝试了其他一些代码。

相关问题