简体   繁体   English

PHP MYSQL BLOB导出为csv格式

[英]PHP MYSQL BLOB export to csv format

I am trying to create a script that can export data from a mysql database containing files (pdf mainly). 我正在尝试创建一个脚本,可以从包含文件(主要是pdf)的mysql数据库中导出数据。 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. 但是,我对BLOB字段有问题,当我使用PHPMYADMIN进行导出和导入时,它工作正常,但是当使用我的脚本时,BLOB字段在顶部添加了附加代码。 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. 当我尝试将此版本导入PHPMYADMIN时,它不起作用。

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 目前,它只是作为行['content']项从数据库中拉出,然后输出到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... 当使用此脚本运行导出时,以下内容将从phpmyadmin运行表的csv导出时输出的实际内容...

%âãÏÓ
%%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^…–ǯ"?

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. 这是因为CSV没有很好地标准化为一种格式(甚至没有像“逗号分隔值”中的“逗号”一样) 这是因为phpmyadmin在导出/导入时会对值进行一些编码/解码。

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. 您需要此编码/解码部分,因为您的二进制BLOB (如您所说的,大多数是PDF)可以轻松地包含逗号,换行符,引号以及各种破坏CSV解析器的内容。

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 . 要使用phpmyadmin导入文件,您必须复制那里使用的编码机制-幸运的是,它是开源的,您可以看一下代码

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. 或者,如果您想要自己的导出/导入机制(比如说:编写与导出程序匹配的自己的导入程序),则可以在此处充分利用base64编码来确保CSV(按原样使用纯文本格式)方式)正确存储二进制数据。

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM