Jump to content

Saving .txt file


eugene2009

Recommended Posts

So i have a generator where the user inputs the variable values and then it is automatically inserted in a code on the next page in a text box. How can I have the user then click on a button that says something like "Save" and will allow them to save that code in a .txt file? Please help

Link to comment
Share on other sites

Heres the code:

 

<?php

$filename = "myfile.txt";

    //first, obtain the data initially present in the text file

    $ini_handle = fopen($filename, "r");

    $ini_contents = fread($ini_handle, filesize($filename));

    fclose($ini_handle);

    //done obtaining initially present data

 

    //write new data to the file, along with the old data

    $handle = fopen($filename, "w+");

        $writestring = "text to write to file\n" . $ini_contents;

        if (fwrite($handle, $writestring) === false) {

            echo "Cannot write to text file. <br />";         

        }

    fclose($handle);

?>

 

 

 

How exactly do I link that with the results? Sorry im new to php. Thank you.

Link to comment
Share on other sites

something like this:

 

$result1 = $_POST[''];
$result2 = $_POST[''];
$result3 = $_POST[''];
$filename = $_POST[''];

<?php

$myFile = "$filename.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$result1 \r\n";
fwrite($fh, $stringData);
$stringData = "$result2 \r\n";
fwrite($fh, $stringData);
$stringData = "$result3 \r\n";
fwrite($fh, $stringData);
fclose($fh);

?>

 

you need to fill in some of the blanks but Ifthis is still not done 7 hours from now I will help you finish the whole script cuz i need to leave

 

-John

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.