I am trying to create a script that can export data from a mysql database containing files (pdf mainly). This data is then imported onto the local version of the system.

However I have a problem with the BLOB field, when I export and import using PHPMYADMIN, it works fine, however when using my script, the BLOB field has additional code added to the top. Almost as if it is code to instruct programs how to deal with it. When i try to import this version into PHPMYADMIN, it doesnt work.

Is is a formatting error, do i need to convert it to a type. At present it is simply pulled from the database as a row['content'] item and then outputted to the csv

Any help would be much appreciated

Regards

///////////////// source code of my script

$file = 'supportingFiles'; // csv name.
$result = mysql_query("SHOW COLUMNS FROM files");
$a = 0;

if (mysql_num_rows($result) > 0) {                                      //print column titles based on number of columns in the two files
while ($row = mysql_fetch_assoc($result)) {
$a++;
}
}

$values = mysql_query("SELECT * FROM files ");          //select client details wehere required

while ($rows = mysql_fetch_array($values)) {                                        //print client information
for ($k=0;$k<$a;$k++) {

$csv_output .= $rows[$k].",";
}
$csv_output .= "\n";            //end of line
}

$filename = $file."_".date("d-m-Y_H-i",time());

header("Content-type: application/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=supportingFiles.csv");

echo $csv_output;           //output data file

when an export is ran using this script the following proceeds the actual content outputted when a csv export of the table is run from phpmyadmin...

%âãÏÓ
%%ISIS AfpToPdf-V.6.2/h3 '2008-05-19 (build:6.20.0.08205)'
4 0 obj
[
/DeviceRGB
]
endobj
5 0 obj
[/Pattern 4 0 R] 
endobj
6 0 obj
[
/DeviceCMYK
]
endobj
7 0 obj
[/Pattern 6 0 R] 
endobj
14 0 obj
<</Length 1221/Filter/FlateDecode>>
stream
xÚ•WÛnã6ýÿümÌ%)’¢úo6Š6Û„ûTÈckW–RYÎ6ýüh‡CJ¶Ûi€\à™áp.gÇ‹|Æ!ÿ6{wÃA@~‚ãçgø3Í    %ph×3ü^ïÿæ[P2cB‚É4Käw?
R<¦èØo-\ïðs³_oüqŽ.yL11°Ø·uåžà}ÕìÜTûáÏüã-\-ó©.ÿ„ÅŸ^þ!Ÿý=óõ‘YÆRƒ=±ÌÀ¬Á$´îÊ”)•TÉ   ±6a¶×JŸúXËY¦ÇJµÖ²äìQ«/]k&ÍyÇœyV›¦gµši{^›0c†¨\c-jÏÆlì!ªÚQ!    Íä^#YrÁóQ™_juÊ5x¶“˜µaâÂYÉôùZ)ëQÛk³ÉY•2~>_¥Ž1×{þ4[ä’ŒÉÅv2<¾ÿâ{Œ_ïªr…ŒžÂð©©„9øú™FFÄZÄ«ª†1Ö        ŒÎ’58Tj˜â‡$áb¯D`²H‡¾
üí</m›ºÛ|¿ƒÕ¦h×n²ã¯—ˆøQc¿¼Ç‰ãvûå·ê k<ÙyJ+¦
"2¦75^‚Ó ãg&x M„ñ4–êØå  wȰ\äå%BÄËØŒ4쉷è÷çßASÃsb¢ñ(¾t_bO¼Gî…›7]Qõe„û¦=ªo:9ø|   Ö”3¾2õØéPü74–š 2qÔYle%Ûn‹ö š{xjö-«U³¯»·5Wáhz‹ª¨Wˆ¿¶ÙÂCëËf¿ƒeœ¥ Ÿ”Ã\{IÆðE   9i=î…    ð3ŠŠß‹§­«;DõÊ•îî¸  4ñþYöóÓÎ^¯<ÁeN$…ö±öG    ¥§÷kÔå”^]¥Xò2¼cˆ}\>¢LdQÅSA7=6RN¾‘¥B—    dXêjë[w{z¿Uâ‡WIÈxVÊK¥`ܬpЗֈx—þ! U¦¨¿ús    ½%ÁBŒœãÃ%؃ï¤ïž«p†ËzMÁaÞ6Ê»†"
iç\lMÏ>ÈL®v‘€šû8ê:Ú<kwh
‰h¶l#ì†ý"ô·+ÊjÇN½ü§7\2¥éû=0å‹¡¾ô½&±HW´ƒ„J^…–ǯ"?
share|improve this question
do you mean cvs or csv? – Mark Baker Jan 30 '12 at 14:17
CSV file mate, it works fone with the built in function in phpmyadmin, but not my script, so i guess phpmyadmin formats the data before exporting somehow? – user1178223 Jan 30 '12 at 14:26
And your script code is...? What additional code does the blob have? – Damien Pirsy Jan 30 '12 at 14:28
I have attached the script i have built and also the addittioanl output in my csv when i run my code, in comparison to the reuslt of the phpmyadmin export – user1178223 Jan 30 '12 at 14:38
It'd be a guess, but I'd suggest that phpmyadmin is base64-encoding your blob fields.... I'd also suggest using PHP's built-in fputcsv() function rather than trying to write it yourself, especially as you're not quoting strings, etc. – Mark Baker Jan 30 '12 at 14:46
show 3 more comments
feedback

1 Answer

that is because CSV is not well standardized as a format (not even the "comma" as in "comma seperated values") and it is because phpmyadmin does some encoding/decoding on the values when exporting/importing.

You need this encoding/decoding part, because your binary BLOB (as you said, mostly PDF) can easily contain commas, line breaks, quotes, and all kinds of stuff that breaks a CSV parser.

To import your files using phpmyadmin, you would have to replicate the encoding machanism used there - lucky you that it's open source and you can have a look at the code.

Alternatively, if you want your own export/import mechanism (lets say: you write your own importer that matches your exporter) then you could make good use of base64 encoding here to ensure your CSV (which is intended as a plain-text format by the way) stores binary data correctly.

exporter:

// convert binary blob to text format
$plaintextdata_for_csv = base64_encode($binarydata_from_blob); 

importer:

// decode text format to binary blob
$binarydata_for_blob = base64_decode($plaintextdata_from_csv); 
share|improve this answer
Thanks for this, starting to narrow down on the issue now. I have now found that when in PHPMYADMIN, if i choose the view as plain text option, rather than actually exporting the values into a csv, the result is exactly the same as the resultant file from my code. Any quick thoughts to save me hunting for hours? Thanks – user1178223 Jan 30 '12 at 15:15
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.