php - 文件不存在但可以 require_once() 它

标签 php windows filesystems require file-exists

start.php

<?php
if(file_exists('/config.php')) require_once('/config.php');
echo TEST;
?>

config.php

<?php
define('TEST','Hamsters');
?>

我有 Windows XP + XAMPP 和 PHP 版本 5.3.8。
如果我运行 start.php,它会给我这个错误:

Notice: Use of undefined constant TEST - assumed 'TEST' in C:\programs\xampp\htdocs\start.php on line 3

现在我将 start.php 修改为以下内容,他给了我我的 Hamsters:

<?php
require_once('/config.php');
echo TEST;
?>

file_exists() 说文件不存在但没有条件仍然能够 require_once() 声称不存在的文件?

最佳答案

做一个要求(或包含)条件是你根本不应该做的事情:

if (file_exists('/config.php')) {
  require_once('/config.php');
}

相反,如果不是必须,则采用 include,如果必须,则采用 require。另见:

将 include/require 包装到条件中会使这些变得复杂,并且由于 include 通常与程序流程相关,因此您确实希望保持简单。

此外,您可能希望稍后使用的一些优化无法通过条件包含实现。

在你的情况下,我想知道你为什么要检查文件是否存在。而不是 require_once 你最肯定的意思是 include_once,if 是多余的:

include_once('/config.php');

关于php - 文件不存在但可以 require_once() 它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18423282/

相关文章:

php - 如何从不同的表中回显值?

windows - DCMTK movescu 错误 : response suceed but no file received

linux - 安装 s3 存储桶后无法查看文件夹详细信息

file - CRC检查文件

php - 带 nohup 的 Shell_exec php

php - 使用 PHP int 的开销是多少?

php - HTML 表单 POST 方法不起作用(尽管显示了 URL 参数)

java - 斯坦福 CoreNLP 仅在 Windows 上失败

windows - 如何使用 windows azure powershell 获取系统时间

Linux 文件系统基准测试最佳实践