使用相同的输入在codeigniter中上传多个图像 [英] uploading multiple image in codeigniter using same input

查看:62
本文介绍了使用相同的输入在codeigniter中上传多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="userfile[]" size="40" multiple/>
    <input type="submit" name="submit" value="Upload">
</form>

控制器

        public function image()
                {
                    $data['error'] = '';
                    $this->load->model('StackM');
                    if(isset($_POST['submit']))
                        {
                            $data['update_pass_error_msg']  = $this->StackM->add_multiple_image();
                        }
                    $this->load->view('stack_view'); 
                }

        Model
        public function add_multiple_image(){
                   if((!empty($_FILES['f2']['name'])))
                    {

                        $config['upload_path']          = 'uploads/';
                        $config['allowed_types']        = 'gif|jpg|png|jpeg';

                        $files = $_FILES;

                        if ($files['f2']['name'][0] == '' )
                            {
                                # code...
                                return "Select a file to upload";
                            }
                        else
                            {
                            $mum_files = count($files['f2']);
                            for($i=0; $i<$mum_files; $i++)
                            {

                                if ( isset($files['f2']['name'][$i]) ) 
                                {


                                    $config['file_name'] = time().'-'.$files['f2']['name'][$i];
                                    $this->load->library('upload', $config);

                                    $_FILES['f2']['name']= $files['f2']['name']["$i"];
                                    $_FILES['f2']['type']= $files['f2']['type']["$i"];
                                    $_FILES['f2']['tmp_name']= $files['f2']['tmp_name']["$i"];
                                    $_FILES['f2']['error']= $files['f2']['error']["$i"];
                                    $_FILES['f2']['size']= $files['f2']['size']["$i"];    

                                    $filename = rand().'-'.$_FILES['f2']['name'];

                                     if (!empty($this->upload->do_upload('f2')))
                                    {
                                         $dataInfo[] = $this->upload->data();

                                         $all_imgs = '';

                                            if ( count($dataInfo) > 0) {
                                                # code...
                                                foreach ($dataInfo as $info) {
                                                    # code...
                                                    $all_imgs .= $info['file_name'];
                                                    $all_imgs .= ',';
                                                }
                                            }
                                    }


                               }
                            }

                        }

                }
                else{
                    $all_imgs = "";
                }
    }



} 
else{
$all_imgs = $this->session->userdata('image');
} 



   $this->db->insert('table_name', $all_imgs);

我在这段代码中面临的问题是想想如果我要添加7张图像,但是它正在显示数据库中只有5张图像,它占用的图像不超过5张,我还想在编辑表单时知道是否不想更改图像,那么它应该保持相同的图像,因此我将旧图像存储在会话中并检查是否存在为空,则只有会话变量。
但是在我的代码中,如果我不上传新图像,则如果我保留旧图像,那么它将保存空白

The problem I am facing in this code is Think suppose if I am adding 7 images, but it's showing only 5 images in database it's not taking more then five and also I want to know while editing the form if I don't want to change the image then it should remain same image so i have stored the old image in session and checking if it is empty then only it should session variable . But In my code if I will not upload new one If I keep old image as then it will save blank

推荐答案

为避免偏移量错误,在for循环内,您需要检查是否设置了数组索引:
这里有一个演示代码,可以在CodeIgniter中上载多个文件:

To avoid the offset error, inside of for loop you need to check if that array index is set or not: Here I have a demo code to upload multiple files in CodeIgniter:

views / stack_view.php

views/stack_view.php

<?php if ($this->session->flashdata('status')) { ?>

    <h5><?=$this->session->flashdata('status')?>: <?=$this->session->flashdata('message')?></h5>

<?php } ?>

<?=form_open_multipart('stack', array('id' => 'my_id'))?>

  <input type="file" name="userfile[]" size="40" multiple/>
  <input type="submit" name="submit" value="Upload">

<?=form_close()?>

controllers / Stack.php

controllers/Stack.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Stack extends CI_Controller {

    public function __construct()
    {
            parent::__construct();

            $this->load->library('session');
            $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
        if ($this->input->post('submit')) {

            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png|jpeg';

            $files = $_FILES;

            if ($files['userfile']['name'][0] == '' ) {
                # code...
                $this->session->set_flashdata('status', 'error');
                $this->session->set_flashdata('message', "Select a file to upload");
            }
            else
            {
                $mum_files = count($files['userfile']);
                $dataInfo = array();
                for($i=0; $i<$mum_files; $i++)
                {

                    if ( isset($files['userfile']['name'][$i]) ) {

                        $config['file_name'] = time().'-'.$files['userfile']['name'][$i];
                        $this->load->library('upload', $config);

                        $_FILES['userfile']['name']= $files['userfile']['name']["$i"];
                        $_FILES['userfile']['type']= $files['userfile']['type']["$i"];
                        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name']["$i"];
                        $_FILES['userfile']['error']= $files['userfile']['error']["$i"];
                        $_FILES['userfile']['size']= $files['userfile']['size']["$i"];    

                        $filename = rand().'-'.$_FILES['userfile']['name'];

                        if ( ! $this->upload->do_upload('userfile'))
                        {
                            $error_message = $this->upload->display_errors();

                            $this->session->set_flashdata('status', 'error');
                            $this->session->set_flashdata('message', "$error_message");
                        }
                        else
                        {
                            //$data = array('upload_data' => $this->upload->data());

                            $this->session->set_flashdata('status', 'success');
                            $this->session->set_flashdata('message', "Files upload is success");
                        }

                        $dataInfo[] = $this->upload->data(); //all the info about the uploaded files are stored in this array

                    }
                }

                //here you can insert all the info about uploaded file into database using $dataInfo
                $all_imgs = '';

                if ( count($dataInfo) > 0) {
                    # code...
                    foreach ($dataInfo as $info) {
                        # code...
                        $all_imgs .= $info['file_name'];
                        $all_imgs .= ',';
                    }
                }

                $insert_data = array(
                   'your_column_name' => rtrim($all_imgs,",")
                );

                $this->db->insert('your_table_name', $insert_data);
            }

        }

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

    }
}

尝试此脚本,希望您将从中获得一些帮助。

Try this script and I hope you will get some help from this.

这篇关于使用相同的输入在codeigniter中上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆