PayPal - IPN页面

i have this issue about IPN Paypal payment via php.

ipn.php

<?php

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
    }
    // post back to PayPal system to validate
    $header = "POST /cgi-bin/webscr HTTP/1.0
";
    $header .= "Content-Type: application/x-www-form-urlencoded
";
    $header .= "Content-Length: " . strlen($req) . "

";

    //$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);


    if (!$fp) {
    // HTTP ERROR
    } else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {

    // PAYMENT VALIDATED & VERIFIED!
        $hour = date("g:ia");
        $fp = fopen("richieste_".date("d_m_Y").".txt", "a");
        fwrite($fp, "1,16,6.01,".$hour.";");
    }

    else if (strcmp ($res, "INVALID") == 0) {

    // PAYMENT INVALID & INVESTIGATE MANUALY!

    }
    }
    fclose ($fp);
    }
?>

The txt file not saved, the ipn status was payment_status=Completed with HTTP request code 200. Where I am wrong?

I always use this approach:

$filename = **FULL_PATH** . "/" . $filename . ".txt";
$handle   = fopen( $filename, "a+" );

If( $handle ) {
  fwrite( $handle, YOUR_CONTENTS_HERE );
}

I can see you don't provide the full path of the file.