0

私はPHPに比較的慣れていないので、小さなスクリプトを実行しようとしています。次の関数を使用してデータを投稿する VB .net プログラムがあります。

Public Sub PHPPost(ByVal User As String, ByVal Score As String)
    Dim postData As String = "user=" & User & "&" & "score=" & Score
    Dim encoding As New UTF8Encoding
    Dim byteData As Byte() = encoding.GetBytes(postData)
    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://myphpscript"), HttpWebRequest)
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.ContentType = "application/x-www-form-urlencoded"
    postReq.ContentLength = byteData.Length
    Dim postReqStream As Stream = postReq.GetRequestStream()
    postReqStream.Write(byteData, 0, byteData.Length)
    postReqStream.Close()
End Sub

ここで、「myphpscript」は実際には PHP スクリプトへの完全な URL です。基本的に、「User」変数と「Score」変数を PHP スクリプトに POST しようとしています。私が試したスクリプトは次のとおりです。

<?php
    $File = "scores.rtf";
    $f = fopen($File,'a');
    $name = $_POST["name"];
    $score = $_POST["score"];
    fwrite($f,"\n$name $score");
    fclose($f);
?>

「scores.rtf」は変更されません。どんな助けでも大歓迎です。前もって感謝します、私はPHPが初めてです。

4

2 に答える 2

0

「scores.rtf」は変更されません。

RTF ファイルは純粋なテキスト ファイルではないため、 RTFファイルでのテキストの表示方法を制御するメタデータとタグが含まれているため、処理方法が異なります。以下の情報源をお読みください。

http://www.webdev-tuts.com/generate-rtf-file-using-php.html

http://blw.de/phprtf_en.php

http://paggard.com/projects/doc.generator/doc_generator_help.html

いずれにせよ、通常のテキスト ファイルが必要な場合は、以下のコードを使用できます。使用しないfwrite()でください。file_put_contents()

file_put_contents("scores.txt", "\n$name $score");
于 2013-05-29T03:30:50.377 に答える
0

スクリプトが POST 変数を受け取っていることを確認します。

http://php.net/manual/en/function.file-put-contents.php

file_put_contents を試すことができます。これは、fopen、fwrite、および fclose の使用を組み合わせたものです。

isset/empty のようなものを使用して、書き込む前に書き込むものがあることを確認するのが賢明かもしれません。

<?php
$file = 'scores.rtf';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= print_r($_POST);

//Once confirmed remove the above line and use below
$current .= $_POST['name'] . ' ' . $_POST['score'] . "\n";

// Write the contents back to the file
file_put_contents($file, $current);
?>

また、RTF の部分を完全に見落としていました。Mahan が言及したことを確認してください。その特定のファイルタイプが必要ない場合は、上記をお勧めします。

于 2013-05-29T03:31:57.740 に答える