Reading and writing multiple text files

I have a form which loads multiple text files into each form element as follows;

'selected disabled><?php $atis_info_file_contents = file_get_contents('../data/ATIS_info.txt');echo($atis_info_file_contents); ?> <?php $filename = '../settings/settings_infoletter.txt'; $eachlines = file($filename, FILE_IGNORE_NEW_LINES); foreach($eachlines as $lines){echo "$lines"; } fclose($filename); ?>

Then when the form is submitted;

<?php if(isset($_POST['submit'])) { $data_infoletter=$_POST['atis_infoletter']; $fp1 = fopen('../data/ATIS_info.txt', 'w'); fwrite($fp1, $data_infoletter); fclose($fp1); } ?>

I have multiple fields and writes as per the above. The however when loading the form doesn’t always load the previous data, and sometimes doesn’t write the new selection.
I read that I may not be able to update multiple text files in one go from the form submit, is that correct, or is there a simple method?

Well, yes and no… First, how did you debug this? Does each line in the file show on the form correctly? Most likely they are run on together. You read each line of the file and then echo it one after another. How do you plan to use that data? Normally, in a file, you use the new-lines to separate the lines.

Also, if the lines include some special characters, they might mess up formatting. You might wish to use a WYSIWYG type of plugin. ( What You See Is What You Get… ) There are hundreds of them. One good one that is fairly simple to use is “TinyMCE” or “CKEditor”. Many others work well too. Here is a link to the best five in case you want to read about them. WYSIWYG plugins

If you want to just save the text to a file, you can just use file_put_contents. In your code, something like this should work: file_put_contents(’…/data/ATIS_info.txt’, filter_input(INPUT_POST, “atis_infoletter”] );
As you see, it also filters the input for programming code and other hacker’s data. You should always
filter your posts since a good hacker can call a post to your site and do damage.

Not sure if I answered your questions… Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service