-1

目前我正在使用 file_get_contents 来获取我拥有的 HTML 页面的内容。

    if(file_exists($fileName)) {
    echo "file exists!";
    $current = file_get_contents("./docs/page1.html");
    echo $current;
} else {
    echo "file doesn't exist";
    file_put_content($fileName, "testing creating a new page"); 
}

当 HTML 文件存在时,我使用 file_get_contents 将内容存储在变量 $current 中,然后回显当前。但是,什么都没有显示。只是一个黑页(我猜是html页面中的css)

如果我不能吐出页面,有没有办法提取html页面的每个元素(divs等)?

4

2 回答 2

1

file_put_content不是函数。试试file_put_contents

另请查看我对您的脚本所做的更改。如果可以创建/读取文件,它将为您提供信息。

<?php 
    $fileName = './docs/page1.html';
    if($current = file_get_contents($fileName)){
        echo "file exists!";
        echo $current;
    } else {
        echo "file doesn't exist";
        if (file_put_contents($fileName, "testing creating a new page")){
            echo 'created a new file!';
        } else {
            echo 'Error, couldnt create file! Maybe I dont have write permissions?';
        }
    }
?>

我测试了这个脚本,它可以工作,除非它没有文件权限。因此,请确保您也对包含该文件的文件夹设置了文件权限。

于 2013-09-06T13:33:51.503 回答
0

您正在显示页面,但页面上可能缺少使用相对路径的内容,例如 CSS 和图像。

如果您只想使用页面的一部分,我建议您查看 DOMDocument。

http://www.php.net/manual/en/domdocument.loadhtml.php

于 2013-09-06T13:29:13.337 回答