3

私は Windows オペレーティング システムを使用しており、PHP は初めてです。つまり、phpファイルにphp関数 fgets() を実装しようとしています:

$file = fopen("$DOCUMENT_ROOT/Chapter02/test.txt","r");

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);

次の内容の test.txt ファイルを作成しました。

こんにちは、これはテストファイルです。
ここには 3 つの行があります。
これが最後の行です。

http://www.w3schools.com/の例

.php と .txt の両方のファイルは、次のディレクトリにあります。

C:/ワンプ/www/Chapter02/

しかし、このphpファイルを実行した後、何も表示されず、次のように表示されます。

こんにちは、これはテストファイルです。
ここには 3 つの行があります。
これが最後の行です。

たぶん「$DOCUMENT_ROOT/Chapter02/test.txt」というパスを間違えているのだと思いますが、本当にどこが間違っているのかわかりません...

私はあなたの助けを非常に感謝します:)

敬具、

イヴァナ

4

2 に答える 2

1

Possible pitfalls:

  • Your document not existing (your $DOCUMENT_ROOT variable may not be set)
  • Your document being empty

fgets() works in exactly the same way across all main PHP platforms, so I am pretty sure that your problem is on the existence of the file rather than the actual reading code (which looks good)

Try to var_dump($DOCUMENT_ROOT) and manually check if the file exists (using file_exists()). This might uncover the bug.

By the way, W3Schools is a horrible website to learn from, as much as I hate to say this. A lot of their info is either out of date, broken, or completely ambiguous.

于 2012-11-18T20:38:28.247 に答える
0

に変更$DOCUMENT_ROOT$_SERVER['DOCUMENT_ROOT']ます。自分で設定しない限り$DOCUMENT_ROOT、サーバー構成から設定された後者を使用する必要があります。

于 2012-11-18T20:42:49.517 に答える