日期:2014-05-17  浏览次数:20640 次

file_get_contents()
这是index.php
PHP code

<?php
$file = './test.php';
$content = file_get_contents($file);
echo $content;


然后test.php中就一句话:this is a test
这两个文件位于同一级目录,我在浏览器中运行时,'this is a test'这句话会被正常打出来,而用命令行运行时却出现了警告,也没有输出'this is a test'.
错误如下:
C:\Users\hahaliu>php G:\www\index.php
PHP Warning: file_get_contents(test.php): failed to open stream: No such file
or directory in G:\www\index.php on line 3

Warning: file_get_contents(test.php): failed to open stream: No such file or di
rectory in G:\www\index.php on line 3
然后我将$file改为了绝对路径
$file = 'G:/www/test.php';
命令行下就能输出'this is a test',为什么用相对路径在命令行下就出错了?我很困惑,是什么没有设置对吗?



------解决方案--------------------
在不考虑php.ini中include_path的指定的情况下:

通过浏览器浏览,也就是作为web server浏览的情况下,文件运行的目录是基于web server配置的

而通过命令行,则是基于系统环境path的

可以通过下面2个输出看出不一样
PHP code


echo getcwd();

echo ini_get('include_path');