-1

HTMLでボタンが押されたときに、テキストを(Macの).txtファイルに単純に書き込もうとしています。これは私が試したことです:

HTML:

<form style="margin-top:70px;" align=center action="write.php" method="post">       
    <input type="submit" value="Write"/>
</form>

PHP:

<?php 
$myFile = "file.txt";
$fh = fopen($file, 'w');
$stringData = "First\n";
fwrite($fh, $stringData);
$stringData = "Second\n";
fwrite($fh, $stringData);
fclose($fh);
?>

すべてのファイルは同じディレクトリにありますが、テキスト ファイルには何も表示されません。どうしたの?

前もって感謝します!

4

1 に答える 1

1

テスト済み

この行を変更

$fh = fopen($file, 'w');

$fh = fopen($myFile, 'w');

ファイルの変数が一致しませんでした。

エラーチェックには、以下を使用することもできます。

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');  //On or Off

以下と併せて:

$fh = fopen($myFile, 'w') or die("Couldn't open file for writing!");

fwrite($fh, $stringData) or die("Couldn't write values to file!");

if時期尚早の書き込みを防ぐために条件を追加することもできます。

PHP ハンドラ

<?php

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');  //On or Off

if(isset($_POST['submit'])){
$myFile = "file.txt";
$fh = fopen($myFile, 'w') or die("Couldn't open file for writing!");
$stringData = "First\n";
fwrite($fh, $stringData) or die("Couldn't write values to file!");
$stringData = "Second\n";
fwrite($fh, $stringData) or die("Couldn't write values to file!");
fclose($fh);

if($fh) {
echo "Data successfully written to file.";
}

}
else {
echo "You cannot do that from here.";
}
?>

HTMLフォーム

name="submit"(送信ボタンに追加)

<form style="margin-top:70px;" align=center action="write.php" method="post">       
    <input type="submit" name="submit" value="Write"/>
</form>
于 2013-10-15T19:44:28.590 に答える