3

我有一个将变量写入文本文件的脚本,我正在使用 fwrite()。这是脚本:

<?php

error_reporting(E_ALL ^ E_NOTICE);

$filename = 'Report.txt';


$batch_id = $_GET['batch_id'];
$status = $_GET['status'];
$phone_number = $_GET['phone_number'];

//check to see if I could open the report.txt
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // I am trying to write each variables to the text file
    if (fwrite($handle, $phone_number) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($phone_number) to file ($filename)";

    fclose($handle);


?>

我在这里有两个问题:

1.当我通过 Batch_ID 收到报告时,我想使用 batch_ID 为每个报告文本文件命名,例如:5626_Report.txt

2.我想将多个变量传递给函数fwrite(),我想在每个$phone_number 旁边写下$status。

4

2 回答 2

4

试试 fprintf。

它类似于 C 的常规 printf,但是您需要将句柄传递给。举个例子:

fprintf($handle, "%s;%s;%s", $batch_id, $status, $phone_number);

或者,您可以使用 PHP 内联字符串并使用:

fwrite($handle "$batch_id;$status;$phone_number");

要解决您的确切问题:

$filename = $batch_id."_report.txt";
$handle = fopen($filename, "a");
fwrite($handle, "$phone_number $status");

那应该有帮助。

于 2012-10-31T21:17:00.527 回答
0

你可以做的更简单:

$status = $_GET['status'];
$phone_number = $_GET['phone_number'];

$file = $_GET['batch_id']."_Report.txt";
//place here whatever you need
$content = "Some content".$status."\n";
file_put_contents($file, $current);
于 2012-10-31T21:20:52.197 回答