douzai6337 2015-04-19 18:03
浏览 21
已采纳

图片上传无法在codeigniter中使用

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

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条回答 默认 最新

  • douxian3170 2015-04-19 18:24
    关注

    Write the below Function in your 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. */
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置