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
https://forums.phpfreaks.com/topic/177039-saving-txt-file/
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
https://forums.phpfreaks.com/topic/177039-saving-txt-file/#findComment-933472
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
https://forums.phpfreaks.com/topic/177039-saving-txt-file/#findComment-933658
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.