我使用下面的代码从mysql数据库上传和检索图像。图像已成功上载并移动到单独的文件夹,但图像路径未存储在数据库中,因此我无法从数据库中检索图像。请检查问题在哪里
if(isset($_POST["submit"])){
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false){
$target="images/".basename($_FILES["image"]["tmp_name"]);
$image = $_FILES['image']['tmp_name'];
$imgContent = addslashes(file_get_contents($image));
$uploadfilename=$_FILES['image']['name'];
/*
* Insert image data into database
*/
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$dataTime = date("Y-m-d H:i:s");
//Insert image content into database
$insert = $db->query("INSERT into images (image,created) VALUES('$imgContent', '$dataTime')");
//move uploaded file to the folder images//
if($uploadfilename!=''){
move_uploaded_file($_FILES["image"]["tmp_name"],$target)
$query="INSERT INTO images SET imgepath='$uploadfilename' ";
mysqli_query($query);
if($query){
{
echo "File uploaded successfully.";
}
}else{
echo "File upload failed, please try again.";
}
}else{
echo "Please select an image file to upload.";
}
}
}
}
?>
答案 0 :(得分:2)
您应该放置图片的文件夹路径或Break when this exception type is user-unhandled
变量而不是$target
。
$imgContent
这也很容易进行SQL注入,使用预处理语句:
"INSERT into images (image,created) VALUES('$target', '$dataTime')"
答案 1 :(得分:2)
将图像名称保存在数据库中并在检索时将图像上传到服务器中,您可以使用文件夹名称后跟图像名称。例如,如果上传图像的文件夹名称是上传
<img src="uploads/<?= $v1['image']?>" alt="product">
&#13;
答案 2 :(得分:0)
感谢您的帮助。下面的代码正在运行。我创建了一个名为&#34; images&#34;使用colms&#34; id&#34;,&#34; name&#34;(我保存了图片的名称)和&#34; url&#34;
<?php
if(isset($_POST['submit']))
{
$host = "...";
$db_name = "....";
$username = "....";
$password = ".....";
$link=mysqli_connect($host, $username, $password, $db_name);
$filename=$_FILES['file']['name'];
$filetmp=$_FILES['file']['tmp_name'];
$target="uploaded/".$filename;
$image_url="http://holo-com.stackstaging.com/uploaded".$filename;
$sql="INSERT INTO images (name,image_url) VALUES ('$filename','$image_url')";
mysqli_query($link,$sql);
if(move_uploaded_file($filetmp,$target))
{ echo "Image uploaded"; }
}
else
{ echo "Failed to upload"; }
?>