图片上传无法在codeigniter中使用

时间:2022-11-24 13:21:48

i am trying to upload multiple images using codeigniter. But images are not uploaded to the upload_images folder.

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

no errors are shown.

没有显示错误。

View form

    <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>

Controller

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');

}

don't know where i made the mistake.

不知道我犯了哪个错误。

Tnx..

1 个解决方案

#1


2  

Write the below Function in your Controller.

在Controller中写下以下功能。

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

call the above Function in your function

在函数中调用上面的函数

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

}

#1


2  

Write the below Function in your Controller.

在Controller中写下以下功能。

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

call the above Function in your function

在函数中调用上面的函数

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

}