21,886
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `word` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(10) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
<?php
function get_conn($config){
$dbhost = $config['host'];
$dbname = $config['dbname'];
$dbuser = $config['user'];
$dbpasswd = $config['password'];
$pconnect = $config['pconnect'];
$charset = $config['charset'];
$dsn = "mysql:host=$dbhost;dbname=$dbname;";
try {
$h_param = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
if ($charset != '') {
$h_param[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset; //設置默認編碼
}
if ($pconnect) {
$h_param[PDO::ATTR_PERSISTENT] = true;
}
$conn = new PDO($dsn, $dbuser, $dbpasswd, $h_param);
} catch (PDOException $e) {
throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31);
}
return $conn;
}
$config = array(
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => '',
'pconnect' => 0,
'charset' => ''
);
$conn = get_conn($config);
// 把文件内容转16进制放入数据表
$file = 'abc.ods';
$type = explode('.',$file);
$type = $type[1];
$data = bin2hex(file_get_contents($file));
$sql = "insert into word(type,content) values('".$type."','".$data."')";
$stmt = $conn->prepare($sql);
$stmt->execute();
// 把文件内容读出,保存为文件
$sql = "select * from word";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
foreach($result as $k=>$row){
print_r($row);
$filedata = pack('H*', $row['content']);
file_put_contents(time().'.'.$row['type'], $filedata, true);
}
?>
$image = pack("H*", substr($data,2));