图片上传无法在codeigniter中使用

时间:2015-04-19 18:03:33

标签: php jquery codeigniter

我正在尝试使用codeigniter上传多张图片。但图像不会上传到upload_images文件夹。

没有显示任何错误。

查看表单

    <form  action="<?php echo base_url(); ?>property/add_images2"  method="post" enctype="multipart/form-data">
        <input type="file" name="files1" multiple="multiple" accept="image/*">
        <div id="image">

        </div>
        <a id="another_image" href="#">Add Another Image</a>
        <input type="submit" value="Upload">
    </form>

jquery的

<script type="text/javascript">

    $(document).ready(function(){
        var count = 2;
        $('#another_image').click (function(){

            if(count < 7){
            $('<input type="file" name="files'+count+'" multiple="multiple" accept="image/*">').appendTo ("#image");
            count++;
            }
        });
        });
    </script>

控制器

function add_images2(){

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

$files = $_FILES;

   if($files){
        for( $i = 1; $i < 7; $i++) {

         $config = array();
            $config['upload_path'] = './upload_images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size']      = '10000';
            $config['file_name'] = 'prop_'.md5(time());
            $config['overwrite']     = FALSE;
            $this->load->library('upload', $config);

            $this->upload->do_upload('files'.$i);
        }
}

    $this->load->view('add_images2');

}

不知道我犯了哪个错误。

TNX ..

1 个答案:

答案 0 :(得分:2)

在控制器中写下以下功能。

/*It will upload the images to the specified path and will return the image urls in an array*/

private function upload_files($path, $files)
{
    $path = PHYSICAL_PATH . $path;
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'JPG|JPEG|GIF|PNG|gif|jpg|png|jpeg|tft|TFT',
        'overwrite'     => 1,                       
    );

    $this->load->library('upload', $config);
    $images = array();      
    foreach ($files['name'] as $key => $image) {
        $_FILES['images[]']['name']= $files['name'][$key];
        $_FILES['images[]']['type']= $files['type'][$key];
        $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images[]']['error']= $files['error'][$key];
        $_FILES['images[]']['size']= $files['size'][$key];

        $fileName = time() .'_'. $image;

        $images[] = $fileName;

        $config['file_name'] = $fileName;

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

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            echo $this->upload->display_errors();die();
            return false;
        }
    }
    return $images;
}

在你的函数中调用上面的函数

$firstFileName = $_FILES['photo']['name'][0];//Check whether any Images is uploaded or not.
if($firstFileName != "")
{   
    $path = "uploads/property_image/";//Give your Path name
    $values = $this->upload_files($path,$_FILES['photo']);
/* $values contents the Image path Array.Now you can send the array to the View Page for your use or can call any Models to store into the DB. */

}