php - 如何在PHP中获取值形式的数组

标签 php mysql codeigniter

我想在 Codeigniter 中上传多张图片,并将上传的图片文件名插入到名为 gallery 的数据库表中。

多张图片上传成功,但我无法获取上传图片的文件名。下面是我的函数

public function products() { 
    $this->load->library('upload');
    $uploadData = array();
    //$fileData = array();
    $files = $_FILES;
    $count = count($_FILES['userfile']['name']);

    for($i=0; $i<$count; $i++) {
        $_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];  

        $imagePath = realpath(APPPATH . '../images/website/gallery');           
        $config['upload_path'] = $imagePath;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name'] = date('Ymd_his_').rand(10,99).rand(10,99).rand(10,99);
        $this->upload->initialize($config);

        if($this->upload->do_upload()) {
            $fileData = $this->upload->data();
            echo "<pre>"; var_dump($fileData); echo "</pre>";
            //inset code will be here               
        } else {
            echo strip_tags($this->upload->display_errors());
        }
    }
}
echo "<pre>"; var_dump($fileData); echo "</pre>"; result is below 

array(14) {
  ["file_name"]=>
  string(26) "20170116_101759_563596.jpg"
  ["file_type"]=>
  string(10) "image/jpeg"
  ["file_path"]=>
  string(43) "H:/Xampp/htdocs/cms/images/website/gallery/"
  ["full_path"]=>
  string(69) "H:/Xampp/htdocs/cms/images/website/gallery/20170116_101759_563596.jpg"
  ["raw_name"]=>
  string(22) "20170116_101759_563596"
  ["orig_name"]=>
  string(26) "20170116_101759_563596.jpg"
  ["client_name"]=>
  string(10) "vision.jpg"
  ["file_ext"]=>
  string(4) ".jpg"
  ["file_size"]=>
  float(28.32)
  ["is_image"]=>
  bool(true)
  ["image_width"]=>
  int(383)
  ["image_height"]=>
  int(291)
  ["image_type"]=>
  string(4) "jpeg"
  ["image_size_str"]=>
  string(24) "width="383" height="291""
}
array(14) {
  ["file_name"]=>
  string(26) "20170116_101759_165983.jpg"
  ["file_type"]=>
  string(10) "image/jpeg"
  ["file_path"]=>
  string(43) "H:/Xampp/htdocs/cms/images/website/gallery/"
  ["full_path"]=>
  string(69) "H:/Xampp/htdocs/cms/images/website/gallery/20170116_101759_165983.jpg"
  ["raw_name"]=>
  string(22) "20170116_101759_165983"
  ["orig_name"]=>
  string(26) "20170116_101759_165983.jpg"
  ["client_name"]=>
  string(22) "Vision-and-Mission.jpg"
  ["file_ext"]=>
  string(4) ".jpg"
  ["file_size"]=>
  float(1950.72)
  ["is_image"]=>
  bool(true)
  ["image_width"]=>
  int(2121)
  ["image_height"]=>
  int(1414)
  ["image_type"]=>
  string(4) "jpeg"
  ["image_size_str"]=>
  string(26) "width="2121" height="1414""
}

我想捕获 ["file_name"]=> 值并动态插入到数据库中。每个图像文件将被一一插入。如果上传了五张或更多图片,那么该数字图片文件名将插入到数据库中。下面是我的示例表。

**id | file_name** 
1  | 20170116_101759_165983.jpg
2  | 20170116_101759_165984.jpg
3  | 20170116_101759_165985.jpg

我尝试了很多教程,但都失败了。

谢谢

最佳答案

从 $fileData 数组中简单获取上传的文件名

    if($this->upload->do_upload()){
                $fileData = $this->upload->data();

             $filename = $fileData['file_name'];
  ///insert this $filename value in db

            }

关于php - 如何在PHP中获取值形式的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673256/

相关文章:

php - MySQL 三表连接——用户和好友图片

mysql - 如何在同一查询中获取同一表的计数和其他列?

php - 如何避免CodeIgniter中的 "mysqli_num_rows() expects parameter 1 to be mysqli_result, object given"

php - 无法在 Codeigniter 中发送电子邮件,没有错误消息。本地或远程

php - 更新包含外键时出错

PHP表单刷新

php - 重新设计页面 : changing the PHP code that generates the HTML

MySQL使用左连接计算查询的行数

mysql - 使用 EB CLI 运行本地 java/mysql 容器堆栈返回 : nc: bad address 'mysql-server'

php - 设置默认 View 值标志时 Magento 的行为很奇怪