[英]How to display an image from a folder using PHP variable in a HTML tag?
I just want to display an image from a directory using PHP.我只想使用 PHP 显示目录中的图像。 But I could not.
但我不能。 I have tried all the below ways.
我已经尝试了以下所有方法。
<?php
/* Getting file name */
$document_root = $_SERVER['DOCUMENT_ROOT'];
$replace = str_replace('/',"\\",$document_root);
$filename = "/excel.png";
//$image_path = $replace."\imagesupload\uploads".$filename;
$image_path = $document_root."/imagesupload/uploads".$filename;
echo $image_path;
//echo '<img src="../admin/upload/' . $display_img . '" width="' . $width . 'px" height="120px"/>';
?>
<html>
<body>
<img src="<?php echo $image_path; ?>" />
</body>
</html>
The above code gives the following output.上面的代码给出了以下输出。 But the path I gave was correct.
但是我给出的路径是正确的。
you are providing a path to file which belongs to server-side and not accessible by the browser , if imageuploads is the root of your application you can do something like this:您正在提供属于服务器端且浏览器无法访问的文件路径,如果 imageuploads 是您的应用程序的根目录,您可以执行以下操作:
<?php
$filename = "excel.png";
?>
<html>
<body>
<img src="/uploads/<?php echo $filename?>" />
</body>
</html>
我想这个会帮助你
<img src="uploads/<?php echo $file.png;?>" />
this should work:这应该有效:
$filename = "/excel.png";
$image_path = "/imagesupload/uploads".$filename;
echo '<img src="' . $image_path. ' "/>';
The DOCUMENT ROOT variable gives the absolute path of the folder in the server machine DOCUMENT ROOT 变量给出了服务器机器中文件夹的绝对路径
DOCUMENT_ROOT=/var/www/example
DOCUMENT_ROOT=C:/wamp/httpdocs
What you're looking for is the relative path on the domain, so you should be using HTTP HOST您要查找的是域上的相对路径,因此您应该使用 HTTP HOST
HTTP_HOST=www.example.com
That way you can build up your image url using the website absolute path and not your computer's absolute path.这样您就可以使用网站绝对路径而不是计算机的绝对路径来构建您的图像 url。
Of course you can always just build a relative path to the folder you're in:当然,您始终可以只构建您所在文件夹的相对路径:
Assuming the script is in the www folder:假设脚本在 www 文件夹中:
<img src='./<?=$filename;?>' />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.