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

时间:2016-05-03 04:49:45

标签: php mysql

我已将图像上传到文件夹并将路径存储到MySQL数据库中。已存储路径,并且已成功将图像插入文件夹。但我的问题是当我从存储在db中的路径显示图像时。它没有显示。当我回显图像路径时,它显示图像路径。我检查了我的浏览器设置,一切都好。这是我的代码。请任何人帮忙。

$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 个答案:

答案 0 :(得分:4)

试试这个:

尝试使用relative path而不是absolute path。相对路径可帮助您从任何服务器,任何目录运行脚本。

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

注意:您需要具有相同的文件夹名称upload/image   脚本运行的目录。根据您的要求,您希望显示来自mysql的图像,但是没有这样做,因为您需要再次查询该表。

答案 1 :(得分:0)

嘿,您可以使用下面的代码在dir中获取上传图片的路径,此代码将显示您的图片

$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.