php 文件路径需要导入页面的语法

发布于 2024-12-19 19:07:47 字数 1046 浏览 3 评论 0原文

我的主目录中有一个名为 imagesabout 的目录以及 3 个 php 文件 和 3 个文件,具有不同的内容

footer.php

This is footer <img src="images/logo.png">

header.php

 <h1>Welcome to MyWebsite</h1>

index.php

<?php
require('header.php'); ?>
Enter your name : and some forms and javascript code
<?php
require('footer.php'); ?>

现在我在目录 about 中有文件作为 about.php

,它有一些内容,下面的代码是

about.php

<?php require('../header.php'); ?>
This is About page<br>
<?php require('../footer.php'); ?>

当我打开时页面 about.php,页脚工作正常,但图像没有显示,并且 image 目录的图像为 logo.png

即使我使用 realpath 来解决相对路径,但无法显示。 即使我也在 footer.php 中尝试过这个,

<?php 
define('__ROOT__', images(__FILE__)); 
?> 

This is footer <img src="<?php echo require_once(__ROOT__.'/logo.png);  ?>">

我也尝试了亲戚路径的可能性,我们还有其他的东西吗?

I have a directory called images and about and 3 php file on home directory
and 3 files and have different content

footer.php

This is footer <img src="images/logo.png">

header.php

 <h1>Welcome to MyWebsite</h1>

index.php

<?php
require('header.php'); ?>
Enter your name : and some forms and javascript code
<?php
require('footer.php'); ?>

Now i have file in directory about as about.php

and it has some contents and below code is

about.php

<?php require('../header.php'); ?>
This is About page<br>
<?php require('../footer.php'); ?>

And when i open the page about.php, the footer is working fine but the image is not showing up, and image directory has image as logo.png

Even i used realpath to work out with relative paths, but could not display.
Even i tried this one too in footer.php

<?php 
define('__ROOT__', images(__FILE__)); 
?> 

This is footer <img src="<?php echo require_once(__ROOT__.'/logo.png);  ?>">

I tried out the possibilites of relatives paths, do we have any other thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄居者 2024-12-26 19:07:47

如果我正确理解你的问题,最简单的解决方案可能就是在 footer.php 中使用图像标签的绝对路径:

所以如果 images 在你的文档根目录中,它看起来像这样:

This is footer <img src="/images/logo.png">

If I'm understanding your question correctly, the easiest solution is probably just to use an absolute path for your image tag in footer.php:

So if images is in your docroot, it would look like this:

This is footer <img src="/images/logo.png">
傲影 2024-12-26 19:07:47

在 php 文件中包含图像绝对没有意义。

您必须了解 PHP 代码和 HTML 代码之间的区别。
PHP 代码在服务器上执行,并将 HTML 代码的结果发送到浏览器
浏览器读取 HTML 并向服务器发出额外请求以获取图像。
因此,服务器文件系统根与浏览器完全无关。

至于问题——特洛特的回答是完美的。

Including images in the php file makes absolutely no sense.

You have to understand the difference between PHP code and HTML code.
PHP code being executed on the server and have result of HTML code sent to the browser.
Where browser reads that HTML and do additional requests to the server to get images.
Thus, server filesystem root has absolutely nothing to do with browser.

As for the problem - Trott's answer is perfect.

青衫负雪 2024-12-26 19:07:47

如果您使用 PHP 来使用 include() 来获取页面的某些部分,那么链接肯定会出现问题。

我相信最好的做法是使用 CSS 调用图像。只需获取一个带有 class 或 id 的 div 并将相对链接放在 CSS 中即可。

这对我有用。

Well if you are using include() to get certain section of the Page with PHP, there will definitely be a problem with links.

I believe the best practice is to call images using CSS. Just get a div with a class or id and place the relative link in the CSS.

It worked for me.

最冷一天 2024-12-26 19:07:47

我得到了这个问题的答案

,将下面的代码复制到 header.php 中,它将在任何地方工作

 <?php
      define('ROOT_DIR', dirname(__FILE__));
      define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
      ?>

并且只要有图像,您只需将下面的代码仅更改图像名称即可,以及任何你想要的。

 <img src="<?php echo ROOT_URL .'/images/logo.jpg'; ?>">

I got the answer for this questions

copy the below code in header.php and it will work every where

 <?php
      define('ROOT_DIR', dirname(__FILE__));
      define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
      ?>

And where ever there is a image you src="" just put the below code only with imagename changed, and whatever you want.

 <img src="<?php echo ROOT_URL .'/images/logo.jpg'; ?>">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文