【问题标题】:Upload Multiple file drag drop codeigniter with multiple category上传具有多个类别的多个文件拖放codeigniter
【发布时间】:2014-09-09 13:01:42
【问题描述】:

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

我的数据库表是

表格类别

id   title

1   test
2   test 2
3   test 2

表格图片

id  image

1   s.jpg

表格图片_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 区域中放置图像时,它将被上传到服务器并将信息发送到数据库。

在我给定的表格结构中,在两个类别下上传了一张图片。 可以是多张图片。

我正在控制器中编写此代码

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 中上传文件与类别

【问题讨论】:

    标签: php codeigniter file-upload dropzone.js


    【解决方案1】:

    dropzone 在每个 ajax 发布请求中上传文件 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>
    

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 2011-07-03
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 2011-09-08
      • 2014-09-26
      相关资源
      最近更新 更多