file_get_contents返回 html 代码的原因


file_get_contents return html code why

我有这个网址:http://localhost/Test/在我的测试中它是一个文件夹内容csv文件,所以我使用了这个函数,但结果它总是内容HTML代码我不明白为什么。

$auth = base64_encode('test:test');
$aContext = array(
  'http' => array(
    'proxy'           => 'tcp://127.0.0.1:80',
    'request_fulluri' => true,
    'header'          => "Proxy-Authorization: Basic $auth",
  ),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("http://localhost/Test/", false, $cxContext);
echo $sFile;

PHP 服务器尝试获取您访问的每个目录index.php,如果要列出CSV文件,则需要创建一个列出文件的index.php

在您的索引中.php

if ($handle = opendir('.')) {
    while (false !== ($csvFile = readdir($handle))) {
        if ($csvFile != "." && $csvFile != "..") {
            echo "$csvFile'n";
        }
    }
    closedir($handle);
}

如果您只需要一个文件,请将直接路径放在 file_get_contents 中。

$sFile = file_get_contents("http://localhost/Test/file.csv", False, $cxContext);