duanliang1019
duanliang1019
2016-05-03 04:49

从mysql中存储的路径显示图像

已采纳

I have uploaded the image into the folder and stored the path into the MySQL Database. The path has been stored and the image has been inserted into the folder successfully. But my problem is when i display the image from the path which is stored in db. It is not displaying. When i echo the image path, it displays the image path. I checked my browser settings, it's all ok. Here is my code. Please anybody help.

$up=move_uploaded_file($_FILES['profile']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/htdocs/upload/image/'.$name);
$path = dirname($_SERVER['DOCUMENT_ROOT']).'/htdocs/upload/image/';
$location = $path . $_FILES['profile']['name'];
$ins=mysqli_query($con, "INSERT into image (url) values ('$location')");
echo 'image uploaded and stored';
echo "$location"; //It displays D:/xampp/htdocs/upload/image/Chrysanthemum.jpg
echo '<img width="250" height="250" src= "'.$location.'"/>';//It doesn't display anything.
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dsd119120 dsd119120 5年前

    Try this:

    Try to use relative path not absolute path. The relative path helps you to run the script from any server, any directory.

    $path = 'upload/image/';
    $location = $path . $_FILES['profile']['name'];
    
    move_uploaded_file($_FILES['profile']['tmp_name'], $location);
    
    $ins = mysqli_query($con, "INSERT into image (url) values ('$location')");
    echo 'image uploaded and stored';
    echo '<img width="250" height="250" src= "'.$location.'"/>';
    

    Note: you need to have a folder name upload/image in the same directory from where the script run. As your requirement, you want to show image from mysql, but did't do it, for that you need to query the table again.

    点赞 评论 复制链接分享
  • duanke1984 duanke1984 5年前

    hey you can use below code to get path of uploaded image in dir this code will show your image

    $location ='/upload/image/'. $_FILES['profile']['name']; // write your project dir name before upload if "upload is not your project dir"
    
    $ins=mysqli_query($con, "INSERT into image (url) values ('$location')");
    echo 'image uploaded and stored';
    
    echo '<img width="250" height="250" src= "'.$location.'"/>';//It doesn't display anything.
    
    点赞 评论 复制链接分享

相关推荐