我想将图像路径插入到数据库表中,以便稍后可以将其显示在不同的位置,但只有插入到表中的内容才是上传图片的文件夹名称,并且图像名称丢失。
这是我的代码:
<?php
require "connect.php";
if(isset($_GET['id']))
{
$id = $_GET['id'];
}
$uploaddir = 'uploaded';
if(isset($_POST['submit']))
{
$filename = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
$filepath = $uploaddir . $filename;
move_uploaded_file($tmp,$filepath);
$filepath=addslashes($filepath);
mysqli_query($conn,"UPDATE vijesti SET imeslike='$filepath' WHERE id='$id'");
mysqli_close($conn);
}
?>
<div id="main">
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" value="upload"/>
</form>
</div>
名为imeslike
的表列应该使用图像路径进行更新,但它只显示uploaded/
。当我在查询中放入一些随机字符串时,它会更新并且字符串值会显示在imeslike
列中。
上传的文件夹位于同一目录中,因此相对路径正常,我也尝试了绝对路径,如:
$uploaddir="C:/wamp/www/admirovsajt/uploaded"
但同样的问题。
答案 0 :(得分:0)
在数据库中查找正确的路径之前验证图像是否已上载。
而不是:
$uploaddir = 'uploaded';
试
$uploaddir = 'uploaded/';
答案 1 :(得分:0)
尝试:
<?php
require "connect.php";
if(isset($_GET['id'])) {
echo $id = $_GET['id'];
}
$uploaddir = 'uploaded/';
if(isset($_POST['submit'])) {
$pid = $_POST['pid'];
$filename = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
$filepath = $uploaddir . $filename;
move_uploaded_file($tmp,$filepath);
$filepath=addslashes($filepath);
mysqli_query($conn,"UPDATE vijesti SET imeslike='$filepath' WHERE id='$pid'");
mysqli_close($conn);
}
?>
<div id="main">
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="pid" value="<?php echo $id;?>">
<input type="file" name="image"/>
<input name="submit" type="submit" value="upload"/>
</form>
</div>