【发布时间】:2023-04-07 08:56:01
【问题描述】:
到目前为止,我已经成功地将上传的图像移动到其指定目录,并将移动图像的文件路径存储到我拥有的数据库中。
但是,问题是我回显的img src 无法读取包含图像文件路径的变量。我一直在花时间验证我的变量的有效性、回显img src 的代码语法以及移动/存储代码的成功执行,但是当我参考视图源时我仍然得到<img src=''应该显示变量中包含的文件路径的页面。
我相信文件路径存储在变量中,因为变量能够被将图像移动到目录和查询到数据库的函数识别。
我的编码和故障排除经验还很年轻,如果我的问题的性质微不足道,请原谅我。
在问这个问题之前,我在 SOF 中搜索了问题,但没有一个答案直接解决了我的问题(至少是我搜索过的问题)。
主 PHP 块
//assigning post values to simple variables
$location = $_POST['avatar'];
.
.
.
//re-new session variables to show most recent entries
$_SESSION["avatar"] = $location;
.
.
.
if (is_uploaded_file($_FILES["avatar"]["tmp_name"])) {
//define variables relevant to image uploading
$type = explode('.', $_FILES["avatar"]["name"]);
$type = $type[count($type)-1];
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rdn = substr(str_shuffle($chars), 0, 15);
//check image size
if($_FILES["avatar"]["size"] > 6500000) {
echo"Image must be below 6.5 MB.";
unlink($_FILES["avatar"]["tmp_name"]);
exit();
}
//if image passes size check continue
else {
$location = "user_data/user_avatars/$rdn/".uniqid(rand()).'.'.$type;
mkdir("user_data/user_avatars/$rdn/");
move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
}
}
else {
$location = "img/default_pic.jpg";
}
HTML 块
<div class="profileImage">
<?php
echo "<img src='".$location."' class='profilePic' id='profilePic'/>";
?><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
查看源代码
<div class="profileImage">
<img src='' class='profilePic' id='profilePic'/><br />
<input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
.
.
.
【问题讨论】: