[英]I Want to Upload an image in database through php,the image is not uploading in database
我嘗試上傳圖像,但是當getimagesize圖像為空時,它返回false ..並且警告即將來臨,它沒有保存在數據庫中。數據庫名稱是project,表名稱是images,字段是name和image。一個代碼...
<?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>
<?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>
編輯此行
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.