无法从avatar_path看到图像

时间:2018-12-01 05:35:48

标签: php mysql

我将图像存储在uploads文件夹中,然后存储在随机目录中,但是未在我的网站上显示,这是我的代码

<?php
$query = "SELECT * FROM users WHERE email='$email' or username = '$email'or mobile='$email'";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
  $users = mysqli_fetch_assoc($fire);

  }
  ?>
<img src="<?php echo $users['avatar_path']?>" width='100' height='100' class='avatar'>

这是我的上传代码

if (isset($_POST['uploadimg'])) {

  $avatar = $_FILES['avatar'];
   $avatar_name = $_FILES['avatar']['name'];
   $avatar_tmpname = $_FILES['avatar']['tmp_name'];
   $avatar_size =  $_FILES['avatar']['size'];
   $avatar_type = $_FILES['avatar']['type'];
   $avatar_ext = pathinfo($avatar_name, PATHINFO_EXTENSION);

   if (!empty($avatar_name)) {
    if ($avatar_size <= 25000000) {
      if ($avatar_ext == "jpg" || $avatar_ext == "jpeg" ||$avatar_ext == "png" ) {
        $chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $rand_dir_name=substr(str_shuffle($chars),0,15);
        mkdir("uploads/$rand_dir_name");
         $final_file= "uploads/$rand_dir_name/$avatar_name";


   $upload = move_uploaded_file($avatar_tmpname, $final_file);
   if ($upload) {
    unlink("$avatar_path");
     $msg = "file uploaded successfully ";
     $query = "UPDATE users SET avatar_path='$final_file' WHERE id='$id'";
     $fire = mysqli_query($con,$query) or die("can not insert file path into database".mysqli_error($con));
     $query = "UPDATE likes SET avatar_path='$final_file' WHERE user_id='$id'";
     $fire = mysqli_query($con,$query) or die("can not insert file path into database".mysqli_error($con));
      $query = "UPDATE photos SET avatar_path='$final_file' WHERE uid='$id'";
     $fire = mysqli_query($con,$query) or die("can not insert file path into database".mysqli_error($con));

     if ($fire) {
       $msg .=" and also inserted into database";

     }



        # code...
      }else{ echo "only jpg,jpeg,png, type format allowed";}
    }else{echo "file size is too large";}

   }else{echo "please select an image to upload";}



  }

}
}
?>

此代码曾经在localhost上运行,我的上传代码仍在工作,当我检查页面时,头像路径是正确的,但仍未显示图片损坏的图像,我不知道这是什么错误即将到来的头像路径

uploads / Un7sL9TwyNzOhco / bhai.jpg

1 个答案:

答案 0 :(得分:0)

尝试先添加斜线,如下所示:

 $final_file= "/uploads/$rand_dir_name/$avatar_name";