Home php教程 php手册 解析php file_exists无效的解决办法

解析php file_exists无效的解决办法

Jun 13, 2016 am 11:44 AM
exists file php superior Method official manual Tutorial method invalid of solve parse

方法1 :据官方手册上描述若php教程的safe mode相关的设置过于苛刻,就会出现这样的情形:尽管文件真实存在也被误报,认为文件不存在。

由于服务器端的php.ini我们无法操纵,当用ini_set()也关闭不了safe mode模式,我们只有退而求其次,找一个更可靠、安全的检测方法来检测文件是否存在。我们可以借助 $_server['document_root'] 来加以实现。$_server['document_root'] 返回的是网站的根目录,该目录的最后一个子目录不包含目录标志符号“/”,如:
d:/www/htdocs
有了根目录,再加上需要检测的文件的路径,我们将得到一个绝对的路径,php就可以顺利使用file_exists()函数对之进行检测。上述代码我们只需要更改第一行为(注意,我们在config.php之前加上了符号“/”):
$file=$_server['document_root']."/config.php";
如此,代码的执行就很可信,不会出现预期以外的结果。
以上方法同样适用于目录(is_dir())或文件(is_file())的相关检测函数,能够检测出被安全保护的目录或文件是否存在。

最后顺便提一下:这类被php特殊设置所保护的文件在引用(include和require)时不必加上$_server['document_root']路径,因为,根据php说明文档,它们是允许引用的。

方法2:本人的情况是因为文件在windows和linux之间来回的移动。造成linux下文件和目录的访问权限被改变,导致出文件除拥有者外其他均无访问权限。使用chmod -r 755 xxx/* 解决了问题。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles