file_exists() 找不到文件

发布于 2024-10-01 16:23:17 字数 1015 浏览 2 评论 0原文

if(file_exists("./squadra/photos/photog.jpg")) {
    echo "### YES ###";
} else {
    echo "### NO ###";
}

如果我在 /zones/team.php 上运行这个函数,它就可以工作(它打印 YES)。如果我在 /auth/ajax.php 上运行此函数,它会打印“否”。为什么?

编辑

所以我做了一些实验。

1 - 如果我尝试:

// file on /zones/team.php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}

// file on /auth/ajax.php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}    

它都说“不”;

2 - 如果我尝试:

// file on /zones/team.php
if(file_exists("./squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}


// file on /auth/ajax.php
if(file_exists("../squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}

两者都说“是”;但是在 team.php 上我使用 ./ 并在 ajax.php ../ 上使用...为什么这有效???

if(file_exists("./squadra/photos/photog.jpg")) {
    echo "### YES ###";
} else {
    echo "### NO ###";
}

if i run this function on /zones/team.php it works (it print YES). If i run this function on /auth/ajax.php it print NO. Why?

EDIT

So i make some experiment.

1 - If i try :

// file on /zones/team.php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}

// file on /auth/ajax.php
if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}    

it says NO on both;

2 - If i try :

// file on /zones/team.php
if(file_exists("./squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}


// file on /auth/ajax.php
if(file_exists("../squadra/photos/provag.jpg")) {
    echo "YES";
} else {
    echo "NO";              
}

it says YES on both; But on team.php im using ./ and on ajax.php ../ ...why this works???

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

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

发布评论

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

评论(7

时光与爱终年不遇 2024-10-08 16:23:17

您的最后一个之所以有效,很可能是因为:

  1. 您正在从驻留在根目录中的 index.php 调用 zones/team.php 。在这种情况下, ./ 部分正确标识了您的当前目录。
  2. 对于ajax,您必须像auth/ajax.php一样直接调用它,而不是像index.php?type=jx&do=auth/ajax这样的东西与No.1相同。因此,情况并非如此,您需要首先使用 ../ 退出 auth,然后继续使用 squadra/... >。

尽可能多地使用绝对路径。相对路径对于 PHP 来说计算它们是一件痛苦的事(在性能方面)。

Your last one works most probably because:

  1. You are calling zones/team.php from an index.php which resides in root. In this case ./ part correctly identifies your current directory.
  2. And for ajax, you must be calling it directly like auth/ajax.php, instead of something like index.php?type=jx&do=auth/ajax which would be the same as No.1. Hence this is not the case, you need to get out of auth first with ../, and then go on with squadra/....

Use absolute paths as often as you can. Relative paths are a pain for PHP to calculate them (in performance-wise).

东风软 2024-10-08 16:23:17

确保您正在考虑您输入的文件夹。文件地址以 / 开头,它是服务器端根目录。如果您想要本地目录,请删除前面的 / 或键入整个路径。

其次确保没有错别字。

祝你好运!

Make sure you are considering the folder that you have typed. You start the file address with / which is server side root. If you want local directory, either remove the preceeding / or type out the entire path.

Secondly make sure you have no typos.

Good luck!

尘世孤行 2024-10-08 16:23:17

当你有一个正斜杠时,file_exist 将转到 HDD 的根目录。

在其前面使用 $_SERVER['DOCUMENT_ROOT'] 或删除斜线并使用 ../ 等。

As you got a forward slash, file_exist will go to the root of the HDD.

Use $_SERVER['DOCUMENT_ROOT'] in front of it or remove the slash and use ../, etc, etc.

暮年 2024-10-08 16:23:17

如果squadra是PHP脚本运行目录下的目录,请尝试

if(file_exists('./squadra/photos/photog.jpg')) {
    echo "### YES ###";
} else {
    echo "### NO ###";
} 

If squadra is a directory under the directory where the PHP script is running, try

if(file_exists('./squadra/photos/photog.jpg')) {
    echo "### YES ###";
} else {
    echo "### NO ###";
} 
—━☆沉默づ 2024-10-08 16:23:17

检查 php safe_mode 状态,
并检查文件路径的大小写敏感性。

php file_exists

警告:
对于由于安全模式限制而无法访问的文件,此函数返回 FALSE。但是,如果这些文件位于 safe_mode_include_dir 中,则仍然可以包含它们。

Check the php safe_mode status,
and check the case sensitivity of the file path.

php file_exists

Warning :
This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

白色秋天 2024-10-08 16:23:17

如果您使用 file_exists 的相对路径,它将返回 false,除非该路径相对于 php 目录。

If you use a relative path with file_exists it returns false unless the path is relative to the php directory.

此生挚爱伱 2024-10-08 16:23:17

再次检查路径 - 我认为前导斜杠是一个错误 - 因为它可能指向根(服务器或更可能的用户空间) - 而您的执行脚本可能位于子路径中...

tl;博士;尝试删除第一个“/”

Check for the path again - I think that the leading slash is a mistake - as it may point to the root (either server or more likely user space) - while your executing script may be located in a sub-path...

tl;dr; Try removing the first "/"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文