上传具有多个类别的多文件拖放codeigniter

时间:2014-09-09 13:01:42

标签: php codeigniter file-upload dropzone.js

我正在使用codeigniter.Now我想使用dropzone-js上传多个类别的多个文件。

我的数据库表是

表类别

id   title

1   test
2   test 2
3   test 2

表格图片

id  image

1   s.jpg

table image _category

id image_id  category _id

1     1         1    
2     1         3 

现在描述我的表格

我来自

<form action="<?php echo site_url('/image/upload'); ?>" class="dropzone dz-clickable">
  <div class="control-group">
    <label class="control-label" for="checkboxes">Inline Checkboxes</label>
    <div class="controls">
     <label class="checkbox inline" for="checkboxes-0">
          <input type="checkbox" name="checkboxes" id="checkboxes-0" value="1">
              test
      </label>
      <label class="checkbox inline" for="checkboxes-1">
         <input type="checkbox" name="checkboxes" id="checkboxes-1" value="2">test 2
      </label>
      <label class="checkbox inline" for="checkboxes-2">
          <input type="checkbox" name="checkboxes" id="checkboxes-2" value="3">test 3</label>   
    </div>

    </div>
    <div class="control-group">

      <div class="dz-default dz-message"><span>Drop files here to upload</span></div>

     </div>  
    </form>

现在,当用户上传图像时,可以选择一个或两个或所有类别。

当用户在dropzone区域中删除图像时,它将在服务器中上传,信息将转到db。

在我给定的表结构中,有一个图像在两个类别下上传。 它可能是多个图像。

我在控制器中编写此代码

class Image extends CI_Controller {

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

    public function index() {
        $this->load->view('dropzone_view');
    }

    public function upload() {
        if (!empty($_FILES)) {
        $tempFile = $_FILES['file']['tmp_name'];
        $fileName = $_FILES['file']['name'];
        $targetPath = getcwd() . '/uploads/';
        $targetFile = $targetPath . $fileName ;
        move_uploaded_file($tempFile, $targetFile);


        }
    }
}

但我无法理解如何使用类别

在db上传文件

1 个答案:

答案 0 :(得分:0)

dropzone在每个ajax post请求中上传文件1,您需要创建将接收这些文件的控制器,并且将逐个上传每个图像,因为您一次上传1个图像。 (例如,我使用Verot上传库进行图像处理)

控制器:

/**
 * Method for drop down upload form server side upload handling
 */
public function upload()
{
    $this->load->model('image_upload_model');
        if (!empty($_FILES))
        {
            $this->image_upload_model->upload_image('1024', './media/wysiwyg/', $_FILES['file']);
        }
}

上传模型:

/**
 * Method for uploading Images from dropdown form.
 *
 * @param $size
 * @param $path
 * @param $file
 */
public function upload_image($size = '', $path = '', $file= '')
{
    $this->load->library('verot_upload');
    $foo = new Verot_upload();
    $new_path_large = '';

    // Upload large image and set $new_path_large
    // as large image location on web folder
    $foo->upload($file);
    if ($foo->uploaded)
    {
        $foo->image_resize = true;
        $foo->image_x = $size;
        $foo->image_ratio_y = true;
        $foo->Process($path);
        if ($foo->processed)
        {
            $new_path_large = substr($foo->file_dst_pathname,1);
        }
    }

    // Create thumbnail from original image and
    // set $new_pat_thumb as thumbnail file location on web folder
    $foo->upload($file);
    if ($foo->uploaded)
    {
        $foo->image_resize = true;
        $foo->image_x = 233;
        $foo->image_ratio_y = true;
        $foo->Process($path.'thumb/');
        if ($foo->processed)
        {
            $new_path_thumb = substr($foo->file_dst_pathname,1);

            // Save data in database
            $this
                ->db
                ->set('date_created', 'NOW()', false)
                ->set('path', $new_path_large, true)
                ->set('thumbnail', $new_path_thumb, true)
                ->insert('wysiwyg_img_uploads');
        }
    }

}

并形成:

<div class="columns large-19 medium-9 small-12">
    <form action="/admin/images/upload"
          enctype="multipart/form-data"
          method="post"
          class="dropzone"
          id="my-awesome-dropzone">
          </form>
</div>
<script src="/skin/js/dropzone.js"></script>