gpt4 book ai didi

php - div 元素的背景图像无法在 php 文件中显示

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:47 24 4
gpt4 key购买 nike

我有一个 .html 文件,它使用 css 来分配 div 的背景图像。

问题是当它是 .html 时它可以工作,我想将它更改为 .php 但我不知道如何显示图像。

我看到了this问题和答案对我没有帮助,使用 url('<?php echo $img;?'><?php $img="bg.gif";?>没用。

这是示例代码:

<?php
$bg = "bg.gif";
?>
<!DOCTYPE html>
<html dir="rtl" lang="ar-sy"> <head>
<meta charset="utf-8">
<style>
html,body {margin: 0px; padding:0px;background-image:url('<?php echo $bg;?>'); }
</style>
</head>
<body>
<div id="wrapper">
<div id="content">
<div id="header">
<div id="logo">
</div>
<div id="links">
</div>
</div>
<div id="mainimg">
</div>
</div>
</div>
</body>
</html>

正文中没有填充图像,尽管我的图像与 php 文件位于同一文件夹中。

那怎么办?

编辑

我尝试了绝对路径,但图像仍然没有显示,而且我正在运行 linux mint 17.1 64 位,“bg.gif”具有它需要的所有权限(组可以读写,其他可以读写也是)。

用我的浏览器(opera)检查,它说“加载资源失败,服务器响应 404 未找到”。

最佳答案

问题已解决,当从我的浏览器检查时,我忘记查看它的外观。

我使用的是 codeignitor ,网站是从 index.php 访问的,所以当你说 $bg="bg.gif" 时,它会在 pathtoyourproject/index.php 下寻找图像/bg.gif,我通过将图像移动到 bg.gif 并放置 $bg="../bg.gif" 来解决它,这样它就不会在 index.php 下查找.

至于绝对路径,如果绝对路径是/var/www/...那么就是在http://localhost/var/www/下面找...这相当于在/var/www/html/var/www/... 下查找,这就是它不起作用的原因。

关于php - div 元素的背景图像无法在 php 文件中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30630384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com