-1

Currently I am using file_get_contents to get contents of an HTML page I have.

    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"); 
}

when the HTML file exists, I store the content using file_get_contents in variable $current and then echo current. However, nothing displays. Just a black page (which I'm guessing is the css in the html page)

If I can't spit out the page, is there a way to extract each element of the html page (divs, etc)?

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 に答える