我收到了将图像路径插入数据库而不是图像文件本身的某些建议。但我仍然有问题,因为它根本不起作用。如果我的代码是这样的,我可以知道我的 SQL 表应该如何设置吗?也请指出我的错误。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
$dbhost = 'localhost';
$dbuser = 'root';
$dbpassword = '';
$dbconnect = mysql_connect($dbhost, $dbuser, $dbpassword) or die("gg");
$dbselect = mysql_select_db('uploadimg', $dbconnect) or die("gg1");
mysql_close($dbconnect);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Image Path Into SQL Database</title>
</head>
<body>
<?php
//getting the filename of the image file.
$filename = $_FILES["image"]["name"];
//directory name to be stored.
$path = "C:/xampp/htdocs/Test/images";
//uploading the image file with the image file name into the directory.
if(move_uploaded_file($_FILES["image"]["tmp_name"],$path."/".$filename)) {
//if the image is stored success into the directory then we are going to store into database.
//the real path with the filename.
$mysql_path = $path."/".$filename;
//sql query to be executed.
$sql = "INSERT INTO uploadimg(filename,path) VALUES ('$filename','$mysql_path')";
//executing the query.
if(mysql_query($sql)) {
echo 'path inserted into database';
}
else {
echo 'path not inserted into database';
}
}
else {
echo 'file not uploaded';
}
}
?>
<form method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload" />
</form>
</body>
</html>