我试图上传图片但是当getimagesize图片为空时它返回false ..并且警告即将到来,它没有保存在数据库中 数据库名称是项目,表名是图像,字段是名称和图像。这是代码......
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/formdata">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//data upload
if( isset($_POST['submit'] ))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE) //image size is checked
{
echo "upload image";
}
else
{
$image= addslashes($_FILES['image']['tmp_name']);
$name=addslashes($_FILES['image']['name']);
$image=file_get_contents($image);
$image= base64_encode($image);
saveimage($name,$image);
}
}
function saveimage($name,$image)
{
$conn = mysql_connect('localhost', 'root','');
mysql_select_db("project",$conn);
$result = mysql_query("insert into images(name,image) values('$name','$image')"); //query implemented
}
?> //function written to save image
</body>
</html>
答案 0 :(得分:0)
<?php
ini_set('mysql.connect_timeout',300);
ini_set('default_socket_timeout',300);
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br><br>
<input type="submit" name="submit" value="upload">
</form>
<?php
// Create connection
$conn = mysql_connect('localhost', 'root', '');
// Check connection
if (mysqli_connect_error()) {
die("Database connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//data upload
if( isset($_POST['submit'] ))
{
mysql_select_db("project",$conn);
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$name=addslashes($_FILES['image']['name']);
$result = mysql_query("insert into images(name,image) values('.$name.','.$image.')");
mysql_close($conn);
}?>
</body>
</html>
答案 1 :(得分:0)
编辑此行
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">