blakestock Posted November 9, 2009 Share Posted November 9, 2009 Hi everyone! I'm new to coding and also with PHP and got stuck on a script I'm writing. Basically I'm trying to send a Hidden form field from a submit button to force a download of the data in the field. What I want it to is create a temporary file name, write the data to that file and spit it out to the browser as a defined name that never changes. What is happening is the temporary file is being created, the file is opening, but there is nothing being written to the file. I even tried writing static info in case the form data wasn't being passed correctly, but that didn't work either. Maybe another set of more experienced eyes can lead me to what I'm doing wrong, I'm sure this can be done with less code but I put some debugging in there to see where the failure is. Code is below: <?php //create a temp file to write data to $tmpFile = tempnam($_ENV['DOCUMENT_ROOT'] . '/files/configs', ''); //Open the template and write it to the temp file, then spit out temp file as intf.conf if (is_writable($tmpFile)) { if (!$handle = @fopen($tmpFile,'a')) { echo "Cannot open file ($tmpFile)"; exit; } if (fwrite($handle, "Testing Writing to file") == FALSE){ echo "Cannot write to file ($tmpFile)"; exit; } //decode string passed from previous page //$config = base64_decode($_POST['dloadconfig']); //$handle = $config; //set headers to output file header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); header('Content-type: application/force-download'); header('Content-Transfer-Encoding: binary'); header('Content-Disposition: attachment; filename="intf.conf"'); header( "Content-Description: File Transfer"); sleep(1); fpassthru($handle); fclose($handle); } else echo "The file $tmpFile is not writable"; //delete temp file from directory unlink($tmpFile); ?> The file that I get prompted to save is being named "intf.conf" but it contains nothing. If I can get it to write the test data of "Testing writing" to the file being downloaded, I'll be able to pass the form data to the file instead of my debugging string and my script will be complete. The reason I need to use temporary file names is because many people could be using this web-app at the same time and it generates info that needs to be unique, and this data will always be different so a static file will not work to download. I guess the simple way to do this would be to have the form data written to a string and then that string be sent to the browser as a file. Thanks in advanced for the help! Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 9, 2009 Share Posted November 9, 2009 Well, you're opening the file in 'a' mode, which is "append writable only". I'm not sure what the stipulations on this file are -- can it be modified once it exists? Does it need to be appended to or should it be rewritten everytime. Regardless, I'm not sure, given the file pointer, that the order of things, vis a vis fpassthru() doesn't need to be different. It could be that you need to: open file fpassthru() write changes to file. It also may be that you need to write what you need to to the file, close it, and reopen it to pass the data back. Regardless of this, you need to open it in a mode different from 'a' Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 9, 2009 Share Posted November 9, 2009 I just perused the fpassthru docs, and it stated that you probably would need to rewrite() the file, so it looks like your code might work, but you will probably need to rewrite the file just before you call fpassthru. Quote Link to comment Share on other sites More sharing options...
blakestock Posted November 9, 2009 Author Share Posted November 9, 2009 Well, you're opening the file in 'a' mode, which is "append writable only". I'm not sure what the stipulations on this file are -- can it be modified once it exists? Does it need to be appended to or should it be rewritten everytime. Regardless, I'm not sure, given the file pointer, that the order of things, vis a vis fpassthru() doesn't need to be different. It could be that you need to: open file fpassthru() write changes to file. It also may be that you need to write what you need to to the file, close it, and reopen it to pass the data back. Regardless of this, you need to open it in a mode different from 'a' Thanks for the reply, to give you move information... The file doesn't need to be opened in append writable only, I don't see why it can't be opened in w+ mode. Everytime my download button is clicked, a new file needs to be created (temp file) and then have the contents of the $_POST['hiddenform'] written to that file. Then pass the file to the browser's output for download. To be honest, I dont even need to write to a file on the server, If I could just have the script pass the contents of my $_POST['hiddenform'] write to a variable and then pass that to the output of the browser, that would work too. I just dont want to run into an issue of 2 people using the script and getting strange output which is why I thought of using temp file names. Thanks! Quote Link to comment Share on other sites More sharing options...
gizmola Posted November 10, 2009 Share Posted November 10, 2009 Yeah, I would consider just passing the data back. This certainly works fine, as people do it all the time with dynamically generated files, such as using the gd library to create an image. With that said, I think we identified the issue with the file opening mode, and fpassthru should work for you, so you can go either way with it. Quote Link to comment Share on other sites More sharing options...
blakestock Posted November 10, 2009 Author Share Posted November 10, 2009 Guess after drawing it out, I found out that I was trying to write to the file pointer and not the file Working now though Thanks for taking a look! 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.