5

fopen我正在编写一个简单的订单系统,其中使用该函数将几个数字(填写在表单中)写入另一个 .php 文件(也可能是 .html) 。这工作正常,但在写入文件后,我希望浏览器实际打开写入的文件,最好是在的浏览器窗口中。这样我的客户可以使用它来打印、用作发票等。

现在我仍然是 php 领域的新手,并且没有使用fopen. 但是在我寻找教程等的所有地方,据说fopen打开(或写入)一个文件,但就我所经历的而言,它并没有。它似乎只是允许访问指定的文件进行写入和读取,而不是实际显示新写入的页面。

为避免任何混淆:我不想像其他关于 SO 状态的问题一样打开链接。

我的代码:

<form action="" method="post">
  <input type="text" id="amountTuna" name="numberTuna" value="0"/>
  <input type="text" id="amountCheese" name="numberCheese" value="0"/>
  <input name="send" id="send" type="submit" value="Post order" />
</form>

<?php
if (array_key_exists('send', $_POST)) { 
  $order = "order.php";
  $fh = fopen($order, 'w') or die("can't open file");//file handler

  fwrite($fh, "Tuna sandwiches: " . stripslashes($_POST['numberTuna']));
  fwrite($fh, "Cheese sandwiches: " . stripslashes($_POST['numberCheese']));

  $fh = fopen($factuur, 'r');
  $fileip = fread($fh, filesize($factuur));
  fclose($fh);
}
?>

尝试不同的fopen参数,如'w','r''r+'似乎没有任何区别。删除fclose($fh)似乎也没有任何区别。

4

3 回答 3

5

使用 JS 脚本打开新窗口。例如,紧随其后fclose($fh)

echo "<script>window.open($order, '_blank'); window.focus();</script>";
于 2012-09-12T14:51:44.187 回答
2

在数据库中存储东西可能会容易得多。

也就是说,使用 fopen 是打开文件本身,在新的浏览器窗口中打开写入的文件将需要一些客户端脚本(即 Javascript)来加载新创建的文件。

谷歌 Javascript window.open()。

汤姆

于 2012-09-12T14:53:54.720 回答
1

既然你只想写一个可打印的页面..你试过file_put_contents

例子

if (isset($_POST['send'])) {
    $file = "test.html";
    $data = "Tuna sandwiches: " . stripslashes($_POST['numberTuna']) . "<br>";
    $data .= "Cheese sandwiches: " . stripslashes($_POST['numberCheese']) . "<br>";
    touch($file);
    file_put_contents($file, $data);
}
于 2012-09-12T14:58:55.393 回答