Ok here’s what I have:

I have a script with a form, when filled in and submit is hit; it will link to another page that will generate a few md5 hashes and add a few words.

I have made it so that all the information generated is placed inside a black box.

Here’s what I need:

Is it possible to output it to a text file instead of a PHP page?

I need it so that when the submit button is hit, a download file pops up. (The text document, with output data in).

OR there is a download link on the same page as the output allowing the user to download the file.

If any of this doesn’t make sense let me know and ill see if I can explain it better.

    Can't you do?

    $f = fopen("file.txt", "w");
    fwrite($f, "TEXT TO WRITE");
    fclose($f);

      Yes that worked fine, now i need this,

      here is my out put from the php script it is an example i quickly made up for the sake of help

      echo "INSERT INTO database (username, password) VALUES ('"; echo $_POST['user']; echo "', '"; echo md5($_POST['pass']); echo "')";

      when user field is typed it ends up at user, and pass md5hash etc

      this then creates an output that will display something like

      INSERT INTO database (username, password) VALUES ('sheephat', '19ae708df0e65fb9d3c9540ac2ab653f')

      What I want is this info to enter directly to the txt file as shown. So when the txt file is opened INSERT INTO database (username, password) VALUES ('sheephat', '19ae708df0e65fb9d3c9540ac2ab653f') is shown.

      Thanks for your help so far.

        Okay, so instead of echo'ing that query, store it as a variable and use that variable in the [man]fwrite/man call... rowanparker's already given you the code.

        Only adjustments you'll probably want to make are (assuming you want the text file to contain more than the most recent query):

        1. Adding a new line after your query

        2. Changing the "w" write mode to the "a" append mode in the [man]fopen/man statement

          $add = "INSERT INTO database (username, password) VALUES ('". $_POST['user']. "', '". md5($_POST['pass'])."')";
          
          $f = fopen("file.txt", "w"); 
          fwrite($f, $add); 
          fclose($f); 

          Heres what i have now. What I now need is something that will delete the file after it is downloaded.

          I have heard that i can use PHP to fetch a file, is it posible that i can fetch the file, make it download, then after the download delete it.

          Im off to read the PHP documentation, would appreciate your input. Thanks

            I changed it again intead, so that all the outputs are put into a folder, fopen() now opens a file.txt based on the username so if i registered it would pop up with sheephat.txt in the /users/ folder.

            I then linked that file for the user to donwload on the output page instead.

            In regards to the delete after download im still interested if anyone knows how I should go about it.

              sheephat wrote:

              In regards to the delete after download im still interested if anyone knows how I should go about it.

              How do the users download the file at present? A direct link?

              What you should do is instead prohibit them from accessing the files directly (via a .htaccess file or even by placing them outside of your website's root folder). Instead, give them a link to a PHP script such as download.php?file=sheephat.txt . In that file, you would then use a function such as [man]readfile/man (or [man]fopen/man and [man]fread/man) along with certain headers so that their browser believes they are downloading a file. The headers I've used are:

              header ('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header ('Content-Type: application/octet-stream');
              header ('Content-Length: ' . filesize($path_to_file));
              header ("Content-Disposition: attachment; filename=\"$filename\"");

              Then, once you're done reading the file's contents, [man]unlink/man it.

                I will try that,

                At the moment the file is generated as $username.txt so for me it would be sheephat.txt and for you it would be bradgrafelman.txt etc

                The link i have set up on the same page which displays download etc. This links the same way. Let me go get some script hang on.

                   $f = fopen("./users/".$user.".txt", "w"); 
                  	fwrite($f, $add); 
                  	fclose($f); 
                  
                  echo "<br><br>";
                  echo "<a href=./users/$user.txt>Alternatively: Download this file (right click, Save target as)</a>";
                  echo "<br><br><br><br>";

                  $add = text that is generated

                    For the purpose of security i would like the file to either be inaccessable (although downloadable using PHP i presume) or deleted directly after it has been downloaded.

                      If you wanted to be secure about it, you could store their username in a SESSION variable and simply link them to your PHP script. No query string would be needed because you could (more) securely retrieve their username from your session data rather than whatever gibberish they added in the URL to try and access someone else's files.

                        I am going to look into making it so that the file is downloadable as you said with the download.php and on completion make it delete the file on the server.

                        That way make downloading the txt the only option. I will send my progress through as i go so other users can see what im tring to do should they want to do the same for some reason.

                          Write a Reply...