在php中使用file函数加载txt文件
我在PHP中加载.txt文件时遇到问题。我的.txt和php文件位于服务器上的同一目录中 目录结构:在php中使用file函数加载txt文件,php,Php,我在PHP中加载.txt文件时遇到问题。我的.txt和php文件位于服务器上的同一目录中 目录结构: /common/stack.php /common/stopwords.txt /abc.php stack.php内容: <?php $stopwords = file('stopwords.txt'); print_r($stopwords); ?> 浏览到:http://localhost/common/stack.php正确打印stopwords.txt
/common/stack.php
/common/stopwords.txt
/abc.php
stack.php内容:
<?php
$stopwords = file('stopwords.txt');
print_r($stopwords);
?>
浏览到:http://localhost/common/stack.php
正确打印stopwords.txt的内容
问题是,当我在abc.php中包含stack.php时,会出现以下错误:
警告:文件(stopwords.txt):无法打开流:没有此类文件或
/Applications/XAMPP/xamppfiles/htdocs/common/stack.php中的目录
这是因为当前工作目录是正在调用的脚本之一——至少在默认情况下是这样 试试这个:
$stopwords = file(__DIR__."/stopwords.txt");
这将从当前(包含)文件的目录加载stopwords文件,这通常对于加载依赖文件非常有用。这是因为当前工作目录是正在调用的脚本之一-至少在默认情况下是如此 试试这个:
$stopwords = file(__DIR__."/stopwords.txt");
这将从当前(包含)文件的目录中加载stopwords文件,这通常对于加载依赖文件非常有用。相对路径可能错误,因为
file()
是相对于/
而不是/common
调用的。相对路径可能错误,因为file()
是相对于/
调用的,而不是/common