Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a file in a new browser window after having written it (with fopen)

Tags:

php

I'm writing a simple order system where several numbers (filled in inside a form) are written to another .php file (may be .html also), using the fopen function. This works fine, but after writing to the file, I want the browser to actually open that written file, preferably in a new browser window. This way my client can use this to print, use as an invoice, etc.

Now I'm still a rookie on php grounds and am not experienced with the use of fopen. But everywhere I look for tutorials etc., it's said that fopen opens (or writes of course) a file, but it doesn't for as far as I've experienced. It just seems to allow access to the specified file to write and read, rather to actually display the newly written page.

To avoid any confusion: I do NOT want to open links like other questions here on SO state.

My code:

<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);
}
?>

Trying different fopen parameters such as 'w','r','r+' etc doesn't seem to make any difference. Removing fclose($fh) doesn't seem to make any difference either.

like image 521
paddotk Avatar asked Sep 12 '12 14:09

paddotk


2 Answers

Use JS script to open new window. For example right after fclose($fh):

echo "<script>window.open($order, '_blank'); window.focus();</script>";
like image 118
StasGrin Avatar answered Oct 01 '22 20:10

StasGrin


Storing things in a database would likely be a lot easier.

That said, using fopen is to open the file itself, opening the written file in a new browser window will require some client-side scripting (i.e. Javascript) to load the newly created file.

Google Javascript window.open().

Tom

like image 38
Thomas Moxon Avatar answered Oct 01 '22 20:10

Thomas Moxon