【问题标题】:How to insert into database 3 arrays files on 3 field如何在 3 个字段上插入数据库 3 个数组文件
【发布时间】:2018-09-10 06:21:45
【问题描述】:

我在上传多张图片的尝试项目中遇到问题。 我不能只使用固定数量的文件来上传。我在 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');  
         }  
 }  

我的上传表单是这个。

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

如何在上面的数据库中修复插入 3 个文件..非常感谢..

【问题讨论】:

    标签: codeigniter upload


    【解决方案1】:

    您正在循环中插入,当您拥有所有个文件时,您想在循环之后插入。

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

    【讨论】:

    • 哦耶..谢谢你的回答.. 但我希望插入文件显示为 3 输入 1 提交,就像我的表单一样
    • 到底发生了什么?
    • 结果是遇到了 PHP 错误 严重性:通知消息:未定义的偏移量:1 文件名:controllers/Upload2.php 行号:77 回溯:文件:C:\xampp\htdocs\asset\ application\controllers\Upload2.php 行:77 函数:_error_handler 文件:C:\xampp\htdocs\asset\index.php 行:315 函数:require_once
    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2017-04-15
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多