我正在尝试将图片从html表单上传到mysql列。我觉得php文件没有接收到基于所有测试的文件,试图找出什么不起作用。
相关HTML代码:
<form action = "insert.php" method="post" enctype="multipart/form-data">
<input type="file" name="myimage">
<input type="submit" name = "Insert" value="Insert">
</form>相关PHP代码:
$imagename=$_FILES["myimage"]["name"];
if(getimagesize($FILES['myimage']['tmp_name']) == FALSE){
echo "no image";
}
//Get the content of the image and then add slashes to it
$imagetmp = addslashes($_FILES['myimage']['tmp_name']);
$image = file_get_contents($imagetmp);
$sql = "INSERT INTO locations (image) VALUES ('$image')";
$res = mysql_query($sql) or die(mysql_error());我相信php没有接收到文件,因为我总是得到我的“无图像”回显。
发布于 2016-08-07 08:30:24
在插入数据库之前,我认为您应该对数据进行base64_encode。
$imagename=$_FILES["myimage"]["name"];
if( getimagesize( $FILES['myimage']['tmp_name'] ) == FALSE ){
exit( "no image" );
}
$image = base64_encode( file_get_contents( $_FILES['myimage']['tmp_name'] ) );要使用mysqli,您可以执行以下操作:
$servername = "localhost";
$username = "foo";
$password = "xxx";
$dbname = "bar";
$db=new mysqli( $servername, $username, $password, $dbname );
$sql='insert into `locations` set `image`=?';
$stmt=$db->prepare( $sql );
$stmt->bind_param( 's', $image );
if( $stmt ) $stmt->execute();
$db->close();
$sql = "INSERT INTO `locations` (`image`) VALUES ('$image')";
$res = mysql_query( $sql ) or die( mysql_error() );发布于 2016-08-07 08:22:22
你面对什么样的错误你没有注意到,我在你的代码中发现了一个问题,我认为应该是工作。
<form action = "insert.php" method="post" enctype="multipart/form-data">警告
这个扩展在PHP5.5.0中被废弃,在PHP7.0.0中被删除。相反,应该使用MySQLi或PDO_MySQL扩展。
发布于 2016-08-07 08:36:01
把图片上传到mysql数据库是个愚蠢的想法。将图像上载到服务器上的文件夹,并将路径(从路径到图像)写入数据库。这是最优的变体,而且更好。
https://stackoverflow.com/questions/38812218
复制相似问题