如何在3字段中插入数据库3数组文件

问题描述 投票:0回答:1

我在上传多个图片的尝试项目中遇到问题。我不能只使用固定数量的文件上传。我在StackOverflow上尝试了很多解决方案,但我找不到工作的解决方案..

我在数据库上的表格格式:enter image description here

这是我的上传控制器

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

   class Upload2 extends CI_Controller {  
     public function __construct() {  
       parent::__construct();  
                         $this->load->helper(array('url','html','form'));  
						 $this->load->model('m_upload');
                 }  
         function index(){  
                 $this->load->view('upload_form');  
         }  
         function upload() {  
                 if($this->input->post('upload'))  
                 {  
						 $foto = array();
                         $number_of_files = sizeof($_FILES['userfiles']['tmp_name']);  
                         $files = $_FILES['userfiles'];  
                                 $config=array(  
                                 'upload_path' => './uploads/', //direktori untuk menyimpan gambar  
                                 'allowed_types' => 'jpg|jpeg|png|gif',  
                                 'max_size' => '2000',  
                                 'max_width' => '2000',  
                                 'max_height' => '2000'  
                                 );  
                         for ($i = 0;$i < $number_of_files; $i++)  
                         {  
                                $_FILES['userfile']['name'] = $files['name'][$i];  
                                $_FILES['userfile']['type'] = $files['type'][$i];  
                                $_FILES['userfile']['tmp_name'] = $files['tmp_name'][$i];  
                                $_FILES['userfile']['error'] = $files['error'][$i];  
                                $_FILES['userfile']['size'] = $files['size'][$i];  
                                $this->load->library('upload', $config);  
                                $this->upload->do_upload('userfile');
								$foto[] = $this->upload->data();
								$data = array(
								
											  //$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
											  'foto'       => $foto[0]['file_name'],
											  'foto_ktp' => $foto[1]['file_name'],
											  'foto_npwp' => $foto[2]['file_name']
											  
											);
											//$this->m_upload->m_upload($data);
											$result_set = $this->m_upload->insert($data);
							
                         }  
                 }  
                         $this->load->view('upload_success');  
         }  
 }  

我的上传表格是This。

 <!DOCTYPE html>  
 <html>  
	 <head>  
			 <title>Tutorial CodeIgniter with Gun Gun Priatna</title>  
	 </head>  
	 <body>  
	 <h2>Upload Gambar</h2>  
			 <?php echo form_open_multipart('index.php/upload2/upload'); ?>  
					 <table>  
							 <tr>  
									 <td>FILE 1<input type="file" name="userfiles[0]" /></td>
									 <td>FILE 2<input type="file" name="userfiles[1]" /></td>
									 <td>FILE 3<input type="file" name="userfiles[2]" /></td>
							 </tr>  
							 <tr>  
									 <td><input type="submit" name="upload" value="upload"></td>  
							 </tr>  
					 </table>  
			 <?php echo form_close();?>  
	 </body>  
 </html>  

如何将insert 3文件修复到上面的数据库中..非常感谢..

codeigniter upload
1个回答
0
投票

您正在循环中插入,当您拥有所有文件时,您希望在循环后插入。

PHP:

function upload() {
    if (!empty($_FILES['userfiles']['name'])) {
        $foto = array();
        $number_of_files = count($_FILES['userfiles']['tmp_name']);
        $files = $_FILES['userfiles'];
        $config = array(
            'upload_path' => './uploads/', //direktori untuk menyimpan gambar  
            'allowed_types' => 'jpg|jpeg|png|gif',
            'max_size' => '2000',
            'max_width' => '2000',
            'max_height' => '2000'
        );
        $this->load->library('upload', $config);
        $foto = array();
        for ($i = 0; $i < $number_of_files; $i++) {
            $_FILES['userfile']['name'] = $files['name'][$i];
            $_FILES['userfile']['type'] = $files['type'][$i];
            $_FILES['userfile']['tmp_name'] = $files['tmp_name'][$i];
            $_FILES['userfile']['error'] = $files['error'][$i];
            $_FILES['userfile']['size'] = $files['size'][$i];
            if (!$this->upload->do_upload('userfile')) {
                show_error($this->upload->display_errors());
            }
            $foto[] = $this->upload->data('file_name');
        }
        $data = array(
            'foto' => $foto[0],
            'foto_ktp' => $foto[1],
            'foto_npwp' => $foto[2]
        );
        $this->m_upload->insert($data);
        $this->load->view('upload_success');
    } else {
        show_error('No files uploaded!');
    }
}

HTML:

没有必要userfiles[2]称他们为userfiles[]

© www.soinside.com 2019 - 2024. All rights reserved.