简体   繁体   English

如何在 HTML 标签中使用 PHP 变量显示文件夹中的图像?

[英]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.

相关问题 如何在Php中将图像设置为变量并使用该变量在HTML中显示图像 - How to Set Image as a Variable in Php and display the image in HTML using that variable 使用php显示文件夹中的单个图像 - Display a single image from folder using php 使用php通过传递给html来显示图像img标签包含从php传递的变量 - Using php to display an image by passing to html img tag contains variables passed from php 如何用php显示文件夹中的图像? - how to display an image from folder with php? 如何在HTML页面中使用php和mysql从数据库显示图像 - how to display image from database using php and mysql in a html page 如何使用php和html显示数据库中的图像 - How to display image from database using php and html 如何使用html输入标签从用户获取图像并将其存储在树莓派上的文件夹中 - How to get an image from a user and store it in a folder on a raspberry pi using the html input tag 如何使用 PHP 从文件夹显示轮播 - How to display carousel from folder using PHP 在html中使用php从文件夹中显示图像 - Using php in html to display images from within a folder 如何使用php将图像或文件存储在文件夹中以及数据库中的该路径,但是图像或文件不是来自html的发布功能 - how to store image or file in a folder and that path in Database using php but image or file is not coming from post functionality of html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM