Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法读取包含文件\u get\u内容的目录中的文件-PHP_Php - Fatal编程技术网

无法读取包含文件\u get\u内容的目录中的文件-PHP

无法读取包含文件\u get\u内容的目录中的文件-PHP,php,Php,我在目录content中有一个文件,其中包含一个要读取的文件。在我的MAMP安装中,content/位于webroot文件夹中(/Applications/MAMP/htdocs/testApp/content)。我无法从文件夹中读取文件(test.txt)。我使用的功能是: function getDataFromLibrary($tgt_url) { //header('Content-Type: text/html; charset=UTF-8'); ec

我在目录
content
中有一个文件,其中包含一个要读取的文件。在我的
MAMP
安装中,
content/
位于webroot文件夹中(
/Applications/MAMP/htdocs/testApp/content
)。我无法从文件夹中读取文件(test.txt)。我使用的功能是:

function getDataFromLibrary($tgt_url) {

        //header('Content-Type: text/html; charset=UTF-8');
        echo "inside get data from lib.";
        if(file_exists($tgt_url)) {
            echo "File does exist.";
            if(is_readable($tgt_url)) {
                echo "file is readable.";
            } else {
                echo "file is not readable.";
            }
        } else {
            echo "File does not exist.";
        }
        echo "tgt url = ".$tgt_url."the end.";
        $file_contents = file_get_contents(urlencode($tgt_url));
        if($file_contents === false) {
            echo "could not read file contents.";
        } else {
            echo "file contents read.";
        }
        echo "Target url=".var_dump($tgt_url)."again the end.";
        echo "File contents = ".$file_contents;
        //$file_contents_replaced = str_replace($file_contents, "\"", "\\\"");
        //echo $file_contents_replaced;
        //var_dump($file_contents);
        //return $tgt_url;
    }
$tgt_url=content/test.txt
时,我得到的输出是:

inside get data from lib.File does exist.file is readable.tgt url = content/test.txtthe end.could not read file contents.string(33) "content/test.txt" Target url=again the end.File contents =  
该文件确实存在,并且可读。我哪里做错了

欢迎任何帮助

编辑:
我在php.ini中打开了以下错误:
display\u errors=On
在脚本中,我包括了以下几行:

error_reporting(E_ALL);
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);  
输出为:

inside get data from lib.File does exist.file is readable.tgt url = content/test.txtthe end.
Warning: file_get_contents(content%2Ftest.txt): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/testApp/index.php on line 323
could not read file contents.string(33) "content/test.txt" Target url=again the end.File contents =
调用urlencode()后,$tgt_url将变为“content%2Ftest.txt”,而不是“content/test.txt”。
您可以在getDataFromLibrary()前面调用urlencode(),它将显示:文件不存在。

当您在开始检查所有内容时,
$tgt\u url
您不应该在所有检查之后更改您所检查的内容。现在,您为
$tgt\u url
检查了文件是否存在等,最后您想得到文件
urlencode($tgt\u url)
什么是不一样的。

不要在file\u get\u contents()函数中使用urlencode()

你有吗?那么你是说在我把它发送到
getDataFromLibrary
之前我应该
urlencode
$tgt\uURL?