从文本生成原始文件

时间:2014-09-04 10:56:26

标签: php

我拥有的是来自数据库的备份数据。看起来像这样:

CREATE TABLE `accFiles` (
  `accfile_id` int(11) NOT NULL AUTO_INCREMENT,
  `accfile_accountId` smallint(5) unsigned NOT NULL,
  `accfile_name` varchar(100) NOT NULL,
  `accfile_type` varchar(100) NOT NULL,
  `accfile_size` int(11) NOT NULL,
  `accfile_content` mediumblob NOT NULL,
  `accfile_extension` varchar(10) NOT NULL,
  PRIMARY KEY (`accfile_id`),
  KEY `IDX_accountId` (`accfile_accountId`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `accFiles` VALUES(1,6,"test.txt","text/plain",27,"test\ntest\ntest\ntest\ntsert","TXT");
INSERT INTO `accFiles` VALUES(2,6,"test.txt","text/plain",27,"test\ntest\ntest\ntest\ntsert","TXT");


CREATE TABLE.....

现在我编写了一个获取内容部分的php脚本,并将其写入一个与3值相同的文件中。编写php是为了获取第一行获取名称和内容,并创建一个具有该名称的文件并将内容写入其中。

<?php
//error_reporting(0);
$file = $argv[1];         //read console parameter
$filep = file_get_contents($file);//get content
$s1="INSERT INTO `accFiles`";     //Search the accfile part
$pos=strpos($filep,$s1);    //first File
$end=strrpos($filep,$s1);   //last file
$ind=0;

do{ // walk through file by file
    $pos2=strpos($filep,$s1,$pos + 5);
    $whant[$ind]=substr($filep,$pos+29,$pos2-$pos-30);
    $ind++;
    $pos=$pos2;
}while(is_numeric($pos2) && $end>=$pos2);


$whant[$ind-1]=substr($filep,$end+29,strpos($filep,"CREATE TABLE",$end)-$end-32); // last file

//echo $whant[0]."\n";
//echo $whant[1]."\n";
$theidd = explode(",",$whant[0]);
$filename = trim($theidd[2],"\"");


$input = substr($whant[0],strpos($whant[0],$theidd[5])+1,strrpos($whant[0],",")-strpos($whant[0],$theidd[5])-2); //get content of file 1

$myfile = fopen(dirname(__FILE__)."/".$filename, "w",1);
fwrite($myfile, $input);
fclose($myfile);
?>

所以我认为问题在于他写的数据与新的txt文件中的数据完全一样,所以内容是test \ ntest \ n的真实新行。 有趣的是,当我有.jpg文件或.gif时,它们的存储方式与内容完全相同,我将在此处发布。 但是我想能够得到原始图片或者它回来的文件。

1 个答案:

答案 0 :(得分:0)

更便携的选择是安装MySQL并导入数据库的备份。然后您可以轻松执行以下操作:

select accfile_name, accfile_content from accFiles

迭代返回的结果并将每个文件写入磁盘,如下面的代码片段所示:

$outputdir = "/home/matthias/output/";
$sql = "select accfile_name, accfile_content from accFiles";
$result = mysql_query($sql, $db_con);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $filename = $row['accfile_name'];
  $content = $row['accfile_content'];
  $path = $outputdir . $filename;
  $file = fopen($path, $content);
  $fclose($file);
}
相关问题