使用PHP代码向MySQL数据库中上传文件时,数据库中无法接到任何内容,但是用控制台向数据库中上传相同文件时就可以上传到数据库里,上传的文件可以正常获取。
HTML文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data" name="mainForm" id="mainForm">
<input type="file" name="myfile" />
<br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
PHP文件:
<?php
header("content-type:text/html;character=utf-8");
$fp = fopen($_FILES["myfile"]["tmp_name"],"rb");
$buf = addslashes(fread($fp,$_FILES["myfile"]["size"]));
$dbh = new PDO("mysql:host = localhost;port = 3306; dbname = test","root","");
if($dbh->query("INSERT picture(picture) VALUES('$buf')"))
echo "图片上传成功";
else
echo "图片上传失败";
$dbh = NULL;
?>