时间:2021-07-01 10:21:17 帮助过:19人阅读
表结构
代码如下:
CREATE TABLE `upload` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(20) NOT NULL,
`data` mediumblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
index.html
代码如下:
post.php
代码如下:
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
$type = $_FILES["file"]["type"];
$size = $_FILES['file']['size'];
$tmp=$_FILES["file"]["tmp_name"];
$fp = fopen($tmp,'rb');
$data = bin2hex(fread($fp,$size));
$dsn='mysql:host=localhost;dbname=test';
echo '
';';
try{
$pdo = new PDO($dsn,'root','root');
$pdo->exec("INSERT INTO `upload`(`type`,`data`) values ('$type',0x$data)");
$id = $pdo->lastInsertId();
echo 'upload success!View';
$pdo = null;
}catch (PDOException $e){
echo $e->getMessage();
}
echo '
view.php
代码如下:
$id = $_GET['id'];
if(is_numeric($id)){
$dsn='mysql:host=localhost;dbname=test';
try{
$pdo = new PDO($dsn,'root','root');
$rs = $pdo->query('select * from `upload` where `id`='.$id);
$row = $rs->fetchAll();
$data = $row[0];
header("Content-Type:${data['type']}");
echo $data['data'];
$pdo = null;
}catch (PDOException $e){
echo $e->getMessage();
}
}else{
exit();
}
http://www.bkjia.com/PHPjc/740205.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/740205.htmlTechArticle大部分人的图片上传都是保存一个路径到数据库,这样在插入时确实快,也符合web的特点,但是在删除时就很麻烦,需要找到文件并删除,...