View Single Post
  #1 (permalink)  
Old Apr 18th, 2008, 20:06
pwbiggs pwbiggs is offline
Junior Member
Join Date: May 2007
Location: London
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
fgetcsv problem with special characters

Hi, I'm reading a csv file and using it to create, insert and modify a MySQL database. I'm trying to write it in such a way as to be as bullet proof as possible and loaded with comments so I can release it to anyone else who wants to do similar.
At least one bullet is getting through
The first line of the csv file is;
PHP: Select all

"FileLocation","FileDirectory","Filename","JFIF_APP1","Main Information","ImageDescription","Make","Model","Software","DateTime","Sub 

Information"
,"ExposureTime","FNumber","ExposureProgram","ISOSpeedRatings","ExifVersion","DateTimeOriginal","DateTimeDigitized","Brigh

tnessValue"
,"ExposureBiasValue","MaxApertureValue","MeteringMode","LightSource","Flash","FocalLength","ImageWidth","ImageHeight","Exp

osureMode"
,"WhiteBalance","DigitalZoomRatio","FocalLength(35mm)","SceneCaptureType","GainControl","Contrast","Saturation","Sharpness"

,"SubjectDistanceRange" 
As you might imagine my database is for images. Anyway,
PHP: Select all

$data fgetcsv($handle$length); 

reads it all in and creates my database OK. But as I say I want to release it and make it as fail safe as possible for myself and others. If one of the first three fields has a ' or a ` in it then fgetcsv reads in the data ok. If two of the fields have those characters it fails. A vardump of $data gives...
Code: Select all
array(4) {
  [0]=>
  string(13) "File`Location"
  [1]=>
  string(13) "FileDirectory"
  [2]=>
  string(9) "Fi'lename"
  [3]=>
  string(0) ""
}
Strangely, if the second ' or ` is later on in the fields list it all works OK
Needless to say I've looked at this all ways up and it's doing my bonce in.
Hope someone can help.
Thanks
Phillip
Reply With Quote