如何保存途中从数据库codeD Base64编码字符串,以文件在磁盘上? [英] How to save encoded Base64 strings from database, to files, on disk?
问题描述
第一篇文章:
我有一个MySQL数据库,它是由该命令创建:
I have a MySQL database, which is created by this command:
CREATE TABLE IF NOT EXISTS
CKox (
A 文本NOT NULL,
b 文本NOT NULL);
CREATE TABLE IF NOT EXISTS
CKox(
aTEXT NOT NULL,
bTEXT NOT NULL );
下面是它的结构:
+----------+-----------------+
| id | base64_str |
+----------+-----------------+
| file_1 | base64_str_1 |
| file_2 | base64_str_2 |
| file_3 | base64_str_3 |
| file_4 | base64_str_4 |
+----------+-----------------+
要保存这些的Base64字符串(数据库)的文件(我的 Web服务器上),我已经运行这个PHP code:
To save these Base64 strings (in database) to files (on my web server), I have run this PHP code:
<?php
// database connection
// ...
$cmd = mysql_query("SELECT * FROM `MyTable`");
while($data = mysql_fetch_array($cmd)) {
$filename = $data[id];
$filedata = $data[base64_str];
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename= ".$filename."");
echo base64_decode($filedata);
}
// ...
?>
它返回的仅一个文件,并有在这个没有数据保存的文件。但是,我想这些的Base64字符串保存到文件中,在服务器磁盘
It return only one file, and there is no data in this saved file. But, I want to save these Base64 strings to files, on server disk.
后得到一些来自人(上StackOverlow)的帮助,我已经更新了我的code,它充满了:
After getting some helps from people (on StackOverlow), I have updated my code, it is full:
// database connection
// ...
$cmd = mysql_query("SELECT * FROM `MyTable`");
while ($data = mysql_fetch_array($cmd)) {
file_put_contents($data['id'], base64_decode($data['base64_str']));
echo $data['base64_str'];
}
// ...
不过,我仍然只有得到一个文件。主要问题是什么?如何解决呢?
But, I still get one file only. What is the main problem? How to solve it?
推荐答案
有关转储文件从数据库到磁盘使用的的file_put_contents
For dump files from db to disk use file_put_contents
$cmd = mysql_query("SELECT * FROM `MyTable`");
while ($data = mysql_fetch_array($cmd)) {
file_put_contents($data['id'], base64_decode($data['base64_str']));
}
这篇关于如何保存途中从数据库codeD Base64编码字符串,以文件在磁盘上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!