我在 win7 上运行 XAMPP 以使用 PHP/MySQL/AJAX 测试相当简单的站点,但遇到了无法正确引用文件的问题。
我知道 C:\xampp\htdocs
与 http://localhost/
“等价”,并且我在 C 中有一个元素子目录:\xampp\htdocs\lorem_ipsum
(即 http://localhost/lorem_ipsum/
)。其中还有一些文件夹(一个用于 php 脚本,一个用于 css 样式表等)。
我在引用这些子目录中的文件时遇到问题。
例如,在 lorem_ipsum/index.php 中,我使用以下行:
require_once 'http://localhost/lorem_ipsum/php/login.php'
导致错误的原因:
Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\xampp\htdocs\lorem_ipsum\index.php on line 13
Warning: require_once(http://localhost/lorem_ipsum/php/login.php): failed to open stream: no suitable wrapper could be found in C:\xampp\htdocs\lorem_ipsum\index.php on line 13
Fatal error: require_once(): Failed opening required 'http://localhost/lorem_ipsum/php/login.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\lorem_ipsum\index.php on line 13
通过谷歌搜索,我了解到这是某种“文件共享/安全”问题,但我完全不知道这意味着什么或如何解决它(我仍然是一个初学者)。
当我使用下面的 CSS 时,例如:
background-image:url('http://localhost/lorem_ipsum/images/bg.png')
背景是白色的,不是'bg.png'。如果我使用 './images/bg.png'
也是一样。
我尝试了各种不同的方式来引用这些文件,但都没有奏效。我还尝试关闭所有防火墙,但也没有用。
我的最终问题是:为什么所有问题都与文件引用有关?
次要问题:PHP 和 CSS 错误是否相关,还是我把它们都搞砸了?我怎样才能阻止这种情况发生? apache/php 中是否有需要更改的设置,或者我只是编码错误?
最佳答案
在 PHP 中,您应该引用指定的 Document_Root 中的文件(在您的例子中为 C:\xampp\htdocs)。所以试试
require_once "/lorem_ipsum/phplogin.php";
如果文件托管正确,css 中的 url 应该 有效。但您也可以尝试使用相对路径,例如:
background-image:url('/lorem_ipsum/images/bg.png');
关于php - 如何在本地主机上为 PHP、CSS 等正确引用子目录(在 xampp 中)中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13538884/