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

时间:2015-07-28 04:36:15

标签: php html5 image

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

然而,问题是我回显的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"/>
                    .
                    .
                    .

2 个答案:

答案 0 :(得分:0)

试试这段代码

<?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>

如果有帮助,请不要忘记将其标记为答案,以便其他人可以轻松获得正确答案。

祝你好运..

答案 1 :(得分:0)

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

更新表后,只需向$location变量声明一个头像会话变量,通过用$location替换所有$_SESSION["avatar_column"]变量来更新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'> ";
                 ?>
        .
        .
        .

谢谢!

相关问题