无法识别文件目录路径的变量


Variable for file directory path not being recognized

到目前为止,我已经成功地将上传的图像移动到其指定的目录,并将移动图像的文件路径存储到我拥有的数据库中。

然而,问题是,我所回显的img src无法读取包含图像文件路径的变量。我一直在花时间验证变量的有效性,在响应img src的代码语法,以及移动/存储代码的成功执行,但是当我参考应该显示变量中包含的文件路径的页面的视图源时,我仍然得到<img src=''

我相信文件路径存储在变量中,因为变量能够被将图像移动到目录和查询到数据库的函数识别。

我的编码和故障排除经验还很不成熟,因此,如果我的问题的性质是琐碎的,请原谅我。

在问这个问题之前,我在soff中搜索了一些问题,但是没有一个答案直接解决了我的问题(至少在我搜索的问题中)。

Main PHP Block

  //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"/>
                    .
                    .
                    .

好了,我终于找到了错误,并能够成功解决它!

只需在更新表后声明一个头像会话变量到$location变量,通过用$_SESSION["avatar_column"]替换所有$location变量来更新html插入,您就设置好了!

PHP:

    $updateCD = "UPDATE users SET languages=?, interests=?, hobbies=?, bio=?, personal_link=?, country=?, avatar=? WHERE email=?";
  $updateST = $con->prepare($updateCD);
  $updateST->bind_param('ssssssss', $lg, $it, $hb, $bio, $pl, $ct, $location, $_SESSION["email_login"]);
  $updateST->execute();
  $_SESSION["avatar"] = $location;   //Important!
  if ($updateST->errno) {
      echo "FAILURE!!! " . $updateST->error;
      }
HTML:

    <div class="profileImage">
                <?php
                $_SESSION["avatar"] = (empty($_SESSION["avatar"])) ? "img/default_pic.jpg" : $_SESSION["avatar"] ;
                 echo "<img src='".$_SESSION["avatar"]."' class= 'profilePic' id='profilePic'> ";
                 ?>
        .
        .
        .

谢谢!

试试这个代码

<?php
error_reporting(E_ALL);
ini_set('display_errors','on');  
$location = ""; //path
if($_POST && $_FILES)
{
  if(is_uploaded_file())
  {
       // your code
      if(<your condition >)
      {
      }
      else
       {
        $location = "./user_data/user_avatars/".$rdn."/".uniqid(rand()).'.'.$type;
        if(!is_dir("./user_data/user_avatars/".$rdn."/"))
        {
           mkdir("./user_data/user_avatars/".$rdn."/",0777,true);
        }
        move_uploaded_file( $_FILES["avatar"]["tmp_name"], $location);
      }
  }
  else
  {
      $location = "img/default_pic.jpg";
  }
}
?>
Html代码:-
<div>
  <?php
  $location = (empty($location)) ? "img/default_pic.jpg" : $location ;
   echo "<img src='".location."' alt='".."'> ";
   ?>
</div>

如果有帮助,不要忘记标记答案,这样别人可以很容易地得到正确的答案。

好运. .