bobthebullet990 Posted January 27, 2009 Share Posted January 27, 2009 I understand how I create a file in php, this is proabably quite an obscure question! but here goes... I have a web page with a few text boxes on it and a submit button. The user enters values into the text boxes and clicks submit.... - The script validates the input data - The script creates a temporary file on the server (temp.conf) - I want to automatically give this file to the user to download after its been generated. Example use: - input valid data - click submit - data is validated and if valid I'm offered to save the file temp.conf somewhere on my local machine - finished. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 27, 2009 Share Posted January 27, 2009 first question...does the server need to have a file called temp.conf on it? or do you just need it on your client system? try this: <?php //validate all the info //generate the output, and store it into $output header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: text/plain" ); header ( "Content-Disposition: attachment; filename=temp.conf" ); header ( "Content-Length: ".strlen($output)); print $output; exit; ?> Quote Link to comment Share on other sites More sharing options...
bobthebullet990 Posted January 28, 2009 Author Share Posted January 28, 2009 Thanks for your reply, I've been playing around, but can't get it to work as I want... I have a variable [$theConf] which stores the file contents, an example of the file contents is... $theConf = "0:0002 9CBD 005b 0002:115.2:001d\n1:0000 E478 005b 0002:115.2:001d"; and then I create a file on the webserver [user enters the filename from a text input on the HTML form]... $theFH = fopen($filename, 'w') or die("can't open file"); fwrite($theFH, $theConf); fclose($theFH); Now I want to prompt the user to save the file to their computer... header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: text/plain" ); header ( "Content-Disposition: attachment; filename=$filename" ); header ( "Content-Length: ".strlen($theConf)); print $theConf; I save the file to the computer and when I open it I see this... <!-- Author: Matt Conway --> <!-- Description: Simple page to create a config file used to configure the test system devices --> <html> <head> <title>Test System Config Tool</title> </head> <body> <center> <h1>Test System Config Tool [under Construction]</h1> 0:0002 9CBD 005b 0002:115.2:001d 1:0000 E478 005b 0002:115.2:001d </center> </body> </html> Which is obviously printing out all the HTML code for the page, on the webserver, if I open the temp.conf file I get the file that I want to give to the user...... 0:0002 9CBD 005b 0002:115.2:001d 1:0000 E478 005b 0002:115.2:001d Currently all I'm doing is just redirecting the browser to the file because this prints the file contents to the web browser which the user can copy and paste into a file, but thats a bit of a pain for the user! Is there a way I can do what I want to do? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 28, 2009 Share Posted January 28, 2009 can you paste the entire code? my guess is that other output is printed before this part: header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: text/plain" ); header ( "Content-Disposition: attachment; filename=$filename" ); header ( "Content-Length: ".strlen($theConf)); print $theConf; Quote Link to comment Share on other sites More sharing options...
bobthebullet990 Posted January 29, 2009 Author Share Posted January 29, 2009 Many thanks, here's the full script as it stands... <!-- Author: Matt Conway --> <!-- Description: Simple page to create a config file used to configure the test system devices --> <html> <head> <title>Test System Config Tool</title> </head> <body> <?php $bauds = array('Select','9.6','14.4','19.2','38.4','57.6','115.2','230.4','460.8','921.6','1382.4'); // Retrieve Data if (isset($_REQUEST['submitConfig'])) { $submit = 1; // flag to see if submit button was pressed $isvalid = 1; // flag to see if the input data is valid or not $bdadd0 = $_REQUEST['BDADD0']; $bdadd1 = $_REQUEST['BDADD1']; $bdadd2 = $_REQUEST['BDADD2']; $bdadd3 = $_REQUEST['BDADD3']; $bdadd4 = $_REQUEST['BDADD4']; $bdadd5 = $_REQUEST['BDADD5']; $bdadd6 = $_REQUEST['BDADD6']; $bdadd7 = $_REQUEST['BDADD7']; $ftrim0 = dechex($_REQUEST['FTRIM0']); $ftrim1 = dechex($_REQUEST['FTRIM1']); $ftrim2 = dechex($_REQUEST['FTRIM2']); $ftrim3 = dechex($_REQUEST['FTRIM3']); $ftrim4 = dechex($_REQUEST['FTRIM4']); $ftrim5 = dechex($_REQUEST['FTRIM5']); $ftrim6 = dechex($_REQUEST['FTRIM6']); $ftrim7 = dechex($_REQUEST['FTRIM7']); $baudr0 = $_REQUEST['BAUDR0']; $baudr1 = $_REQUEST['BAUDR1']; $baudr2 = $_REQUEST['BAUDR2']; $baudr3 = $_REQUEST['BAUDR3']; $baudr4 = $_REQUEST['BAUDR4']; $baudr5 = $_REQUEST['BAUDR5']; $baudr6 = $_REQUEST['BAUDR6']; $baudr7 = $_REQUEST['BAUDR7']; $filename = $_REQUEST['FILENM']; // TODO - Validate the bluetooth addresses // TODO - Build the string to write in the file - for now, lets just use an example... $theConf = "0:0002 9CBD 005b 0002:115.2:001d\n1:0000 E478 005b 0002:115.2:001d"; // Create temporary file and offer the user to save it locally on their client machine $theFH = fopen($filename, 'w') or die("can't open file"); fwrite($theFH, $theConf); fclose($theFH); $configComplete = 1; } else { $submit=0; // submit button not pressed $configComplete = 0; // the config file is not complete } // Validate input data ?> <center> <h1>Test System Config Tool [under Construction]</h1> <?php if (!$configComplete) { ?> <form name="theform" method="post" action=""> <table> <tr> <td></td> <td><p>Bluetooth Address</p></td> <td></td> <td><p>Crystal Frequency</p></td> <td></td> <td><p>Baud Rate</p></td> </tr> <tr> <td><p>Device 0 --> [0002 5b]</p></td> <td><input type="text" name="BDADD0" value="<?php if ($submit) echo $bdadd0; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM0" value="<?php if ($submit) echo hexdec($ftrim0); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR0"><?php foreach ($bauds as $current) { if ($current==$baudr0) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 1 --> [0002 5b]</p></td> <td><input type="text" name="BDADD1" value="<?php if ($submit) echo $bdadd1; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM1" value="<?php if ($submit) echo hexdec($ftrim1); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR1"><?php foreach ($bauds as $current) { if ($current==$baudr1) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 2 --> [0002 5b]</p></td> <td><input type="text" name="BDADD2" value="<?php if ($submit) echo $bdadd2; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM2" value="<?php if ($submit) echo hexdec($ftrim2); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR2"><?php foreach ($bauds as $current) { if ($current==$baudr2) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 3 --> [0002 5b]</p></td> <td><input type="text" name="BDADD3" value="<?php if ($submit) echo $bdadd3; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM3" value="<?php if ($submit) echo hexdec($ftrim3); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR3"><?php foreach ($bauds as $current) { if ($current==$baudr3) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 4 --> [0002 5b]</p></td> <td><input type="text" name="BDADD4" value="<?php if ($submit) echo $bdadd4; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM4" value="<?php if ($submit) echo hexdec($ftrim4); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR4"><?php foreach ($bauds as $current) { if ($current==$baudr4) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 5 --> [0002 5b]</p></td> <td><input type="text" name="BDADD5" value="<?php if ($submit) echo $bdadd5; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM5" value="<?php if ($submit) echo hexdec($ftrim5); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR5"><?php foreach ($bauds as $current) { if ($current==$baudr5) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 6 --> [0002 5b]</p></td> <td><input type="text" name="BDADD6" value="<?php if ($submit) echo $bdadd6; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM6" value="<?php if ($submit) echo hexdec($ftrim6); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR6"><?php foreach ($bauds as $current) { if ($current==$baudr6) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 7 --> [0002 5b]</p></td> <td><input type="text" name="BDADD7" value="<?php if ($submit) echo $bdadd7; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM7" value="<?php if ($submit) echo hexdec($ftrim7); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR7"><?php foreach ($bauds as $current) { if ($current==$baudr7) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> </table> <br><br> <table> <tr> <td><p>Filename: </p></td> <td><input type="text" name="FILENM" value="<?php if ($submit) echo $filename; ?>"</td> <td><input type="submit" value="Save Config" name="submitConfig"></td> </tr> </table> </form> <!-- The config file has been written to file, so give the user the file to download --> <?php } else { header ("Location: $filename"); header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: text/plain" ); header ( "Content-Disposition: attachment; filename=$filename" ); header ( "Content-Length: ".strlen($theConf)); print $theConf; } ?> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
bobthebullet990 Posted January 29, 2009 Author Share Posted January 29, 2009 Sorry, in the above file, I've just noticed that I've left in a line that shouldn't be there... [Line 155] header ("Location: $filename"); Cheers. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 as i suspected...your logic is out of wack. when the form is submitted, it will print out all that HTML that is before the PHP...which is no good. also, use $_POST instead of $_REQUEST: <?php /** * Author: Matt Conway * Description: Simple page to create a config file used to configure the test system devices */ $bauds = array('Select','9.6','14.4','19.2','38.4','57.6','115.2','230.4','460.8','921.6','1382.4'); if($_SERVER['REQUEST_METHOD'] == 'POST'){ //The form has been submitted $bdadd0 = $_POST['BDADD0']; $bdadd1 = $_POST['BDADD1']; $bdadd2 = $_POST['BDADD2']; $bdadd3 = $_POST['BDADD3']; $bdadd4 = $_POST['BDADD4']; $bdadd5 = $_POST['BDADD5']; $bdadd6 = $_POST['BDADD6']; $bdadd7 = $_POST['BDADD7']; $ftrim0 = dechex($_POST['FTRIM0']); $ftrim1 = dechex($_POST['FTRIM1']); $ftrim2 = dechex($_POST['FTRIM2']); $ftrim3 = dechex($_POST['FTRIM3']); $ftrim4 = dechex($_POST['FTRIM4']); $ftrim5 = dechex($_POST['FTRIM5']); $ftrim6 = dechex($_POST['FTRIM6']); $ftrim7 = dechex($_POST['FTRIM7']); $baudr0 = $_POST['BAUDR0']; $baudr1 = $_POST['BAUDR1']; $baudr2 = $_POST['BAUDR2']; $baudr3 = $_POST['BAUDR3']; $baudr4 = $_POST['BAUDR4']; $baudr5 = $_POST['BAUDR5']; $baudr6 = $_POST['BAUDR6']; $baudr7 = $_POST['BAUDR7']; $filename = $_POST['FILENM']; // TODO - Build the string to write in the file - for now, lets just use an example... $theConf = "0:0002 9CBD 005b 0002:115.2:001d\n1:0000 E478 005b 0002:115.2:001d"; // Create temporary file and offer the user to save it locally on their client machine $theFH = fopen($filename, 'w') or die("can't open file"); fwrite($theFH, $theConf); fclose($theFH); header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: text/plain" ); header ( "Content-Disposition: attachment; filename=$filename" ); header ( "Content-Length: ".strlen($theConf)); print $theConf; exit; } ?> <html> <head> <title>Test System Config Tool</title> </head> <body> <center> <h1>Test System Config Tool [under Construction]</h1> <form name="theform" method="post" action=""> <table> <tr> <td></td> <td><p>Bluetooth Address</p></td> <td></td> <td><p>Crystal Frequency</p></td> <td></td> <td><p>Baud Rate</p></td> </tr> <tr> <td><p>Device 0 --> [0002 5b]</p></td> <td><input type="text" name="BDADD0" value=""></td> <td><p> - </p></td> <td><input type="text" name="FTRIM0" value=""></td> <td><p> - </p></td> <td> <select name="BAUDR0"> <?php foreach ($bauds as $current) { echo " <option value=\"$current\" $selected>$current</option>\n"; } ?> </select> </td> </tr> <tr> <td><p>Device 1 --> [0002 5b]</p></td> <td><input type="text" name="BDADD1" value="<?php if ($submit) echo $bdadd1; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM1" value="<?php if ($submit) echo hexdec($ftrim1); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR1"><?php foreach ($bauds as $current) { if ($current==$baudr1) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 2 --> [0002 5b]</p></td> <td><input type="text" name="BDADD2" value="<?php if ($submit) echo $bdadd2; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM2" value="<?php if ($submit) echo hexdec($ftrim2); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR2"><?php foreach ($bauds as $current) { if ($current==$baudr2) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 3 --> [0002 5b]</p></td> <td><input type="text" name="BDADD3" value="<?php if ($submit) echo $bdadd3; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM3" value="<?php if ($submit) echo hexdec($ftrim3); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR3"><?php foreach ($bauds as $current) { if ($current==$baudr3) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 4 --> [0002 5b]</p></td> <td><input type="text" name="BDADD4" value="<?php if ($submit) echo $bdadd4; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM4" value="<?php if ($submit) echo hexdec($ftrim4); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR4"><?php foreach ($bauds as $current) { if ($current==$baudr4) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 5 --> [0002 5b]</p></td> <td><input type="text" name="BDADD5" value="<?php if ($submit) echo $bdadd5; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM5" value="<?php if ($submit) echo hexdec($ftrim5); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR5"><?php foreach ($bauds as $current) { if ($current==$baudr5) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 6 --> [0002 5b]</p></td> <td><input type="text" name="BDADD6" value="<?php if ($submit) echo $bdadd6; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM6" value="<?php if ($submit) echo hexdec($ftrim6); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR6"><?php foreach ($bauds as $current) { if ($current==$baudr6) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> <tr> <td><p>Device 7 --> [0002 5b]</p></td> <td><input type="text" name="BDADD7" value="<?php if ($submit) echo $bdadd7; ?>"></td> <td><p> - </p></td> <td><input type="text" name="FTRIM7" value="<?php if ($submit) echo hexdec($ftrim7); ?>"></td> <td><p> - </p></td> <td><select name="BAUDR7"><?php foreach ($bauds as $current) { if ($current==$baudr7) { $selected = 'selected'; } else { $selected = ""; } echo "<option value=\"$current\" $selected>$current</option>"; } ?></select></td> </tr> </table> <br><br> <table> <tr> <td><p>Filename: </p></td> <td><input type="text" name="FILENM" value="<?php if ($submit) echo $filename; ?>"</td> <td><input type="submit" value="Save Config" name="submitConfig"></td> </tr> </table> </form> <!-- The config file has been written to file, so give the user the file to download --> </center> </body> </html> p.s. - this still assumes you need the file created on the server...if you don't need the file on the server (aka you just needed it downloaded) remove the fopen/fwrite/fclose part Quote Link to comment Share on other sites More sharing options...
bobthebullet990 Posted January 29, 2009 Author Share Posted January 29, 2009 Very good, many thanks for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.