file_get_contents()解决方案

WBOY
Release: 2016-06-13 13:49:08
Original
1694 people have browsed it

file_get_contents()
这是index.php

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

Copy after login

然后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'); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template