使用PHP获取CSV文件中包含逗号的值

时间:2015-02-15 08:57:19

标签: php csv

我有一个包含这样的值的csv

EX-122PD,XXX,Black,5,"8,2",no_picture_95.png

这是我的代码

if(!file_exists('./csv/'))
   mkdir('./csv/');
$fl_name = $_FILES['img']['name'];
$file= 'csv/'.md5(uniqid(rand())).$_FILES['img']['name'];
$fl="./".$file;
move_uploaded_file($_FILES['img']['tmp_name'],$fl);
// Open a known directory, and proceed to read its contents
foreach(glob($fl) as $file)
{
   $fileTemp = $file;
   $fp = fopen($fileTemp,'r');
   $datas = array();
   $timestamp = time();
   while (($data = fgetcsv($fp,1000, ",")) !== FALSE)
   {
       $timestamp = intval($timestamp + 10);
       $data = array(
                     'model_title' => trim($data[0]),
                     'model_series' => trim($data[1]),
                     'color' => trim($data[2]),
                     'brand_id' => trim($data[3]),
                     'category_id' => rtrim(ltrim($data[4],'""'),'""'),
                     'model_image' => 'model_images/'.trim($data[5]),
                     'model_timestamp' => $timestamp,
                     );
         $data['msg']=$this->autoload_model->insert_table('td_model',$data,$timestamp);
   }
}

但我将我的数据插入

|-----------------------------------------------------------------------------------------------------------------|
| model_title  |   model_series  |  color  |  brand_id  |   category_id  |   model_image       | model_timestamp  |
|-----------------------------------------------------------------------------------------------------------------|
|  EX-122PD    |      XXX        |  Black  |    5       |        8       | model_iamges/1.jpg  |    1423989807    |
|-----------------------------------------------------------------------------------------------------------------| 

在类别字段中,它应该是8,2但我只得到8

我做错了什么?

0 个答案:

没有答案