douduocuima61392 2015-07-28 04:36
浏览 70
已采纳

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

so far I've successfully moved an uploaded image to its designated directory and stored the file path of the moved image into a database I have.

Problem is, however, is that the img src I have echoed fails to read the variable containing the file path of the image. I've been spending time verifying the validity of my variables, the code syntax in echoing the img src, and the successful execution of the move/storing code, but I still get <img src='' when I refer to the view source of the page that is supposed to display the file path contained in the variable.

I believe the file path is stored within the variable because the variable was able to be recognized by the functions that both moved the image to a directory and the query to database.

My coding and troubleshooting experience is still very adolescent, thus pardon me if the nature of my question is bothersomely trivial.

Before asking this question, I've searched for questions within SOF but none of the answers directly addressed my issue (of the questions I've searched at least).

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 Block

              <div class="profileImage">
                <?php

                  echo "<img src='".$location."' class='profilePic' id='profilePic'/>";

                ?><br />
                  <input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
                    .
                    .
                    .

View Source

<div class="profileImage">
                <img src='' class='profilePic' id='profilePic'/><br />
                  <input type="file" name="avatar" id="avatar" accept=".jpg,.png,.jpeg"/>
                    .
                    .
                    .
  • 写回答

2条回答 默认 最新

  • dongqian1925 2015-07-29 06:31
    关注

    Alright, I've finally found the error and was able to successfully solve it!

    Simply declare a avatar session variable to the $location variable after updating the table, update the html insert by replacing all $location variables with $_SESSION["avatar_column"] and you are set!

    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'> ";
                     ?>
            .
            .
            .
    

    Thank you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求