Log in

View Full Version : code writes to single file, want to write to selected file



?foru
04-10-2009, 04:01 AM
The following writes to a single text file, but I would like to change it to write to the selected file (file1, file2...so on) so more than one area can be updated. (only one area edited at a time though)

<?php
$data_file='content.txt';
$changes='';

if(isset($contents)) {
$archive=fopen($data_file,'r+');
flock($archive,2);
ftruncate($archive,0);
fputs($archive,stripslashes($contents));
flock($archive,3);
fclose($archive);
unset($contents);
$changes='<span class="iheadline">Changes made!</span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/">View Changes [+]</a><br />';
}

function getfile($filename) {
$fd = fopen($filename, "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}

$data=getfile($data_file);
?>
The idea scenario would be to have a dropdown box with the files listed so you can select the one you want to edit.


$data_file = array('file1' => 'file1.txt', 'file2' => 'file2.txt', 'file3' => 'file3.txt', 'file34' => 'file4.txt');

I changed $data_file to an array but I'm not sure how it would all tie together to write to the selected file.

Select dropdown can possibly look like this.

<SELECT name="data_file">
<OPTION value="file1">edit file1</OPTION>
<OPTION value="file2">edit file2</OPTION>
<OPTION value="file3">edit file3</OPTION>
<OPTION value="file4">edit file4</OPTION>
</SELECT>

Any ideas/help would be appreciated. Thank you.

CrazyChop
04-10-2009, 11:10 AM
$selected_files = array ("file1", "file2");


foreach($selected_files as $data_file)
{
$content = get_file($data_file);
$archive=fopen($data_file,'r+');
flock($archive,2);
ftruncate($archive,0);
fputs($archive,stripslashes($contents));
flock($archive,3);
fclose($archive);
unset($contents);
$changes='<span class="iheadline">Changes made!</span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/">View Changes [+]</a><br />';
}

?foru
04-12-2009, 01:32 AM
Thank you CrazyChop

This seems like a nice short piece of code to make this work.
foreach($selected_files as $data_file)

When you login to edit files it uses the following URL
index.php?status=connected

What might be the best way to determine which file to edit?

index.php?status=connected&selected_files=file1.txt
with something like...

if (isset($_GET['selected_files'])) {
$selected_files = $_GET['selected_files'];
}
to get the page from the URL? $selected_files already sets the array in your code, so the above may not work. Thank you

borris83
04-12-2009, 03:44 AM
Can you try the following? Create files content.txt, file2.txt, file3.txt, file4.txt in a directory and save the following as a php page in the same directory...

(I am not sure that this is what you exactly want to do.. But I haen't tested yet)





<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = GET>

<table>
<tr><td><b>Select a file to edit:</b></td></tr>
<tr><td>
<SELECT name="data_file">
<OPTION value="content.txt">edit file1</OPTION>
<OPTION value="file2.txt">edit file2</OPTION>
<OPTION value="file3.txt">edit file3</OPTION>
<OPTION value="file4.txt">edit file4</OPTION>
</SELECT>
</td></tr>

<tr><td>
<textarea rows = 3 columns = 20 name = "contents" ></textarea>
</td></tr>

<tr><td><b><input type = "submit" name = "submit" value = "Edit" ></b></td></tr>

</table>


</form>



<?php

function getfile($filename) {
$fd = fopen($filename, "rb");
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}



if(isset($_GET['submit']))
{
$data_file = $_GET['data_file'];
$contents = $_GET['contents'];


$archive = fopen($data_file,'r+');
flock($archive,2);
ftruncate($archive,0);
fputs($archive,stripslashes($contents));
flock($archive,3);
fclose($archive);
unset($contents);
$changes='<span class="iheadline">Changes made!</span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="' . $data_file . '">View Changes [+]</a><br />';
echo $changes;


$data=getfile($data_file);


}

?>

?foru
04-15-2009, 02:27 AM
Thank you borris83.

This is working somewhat...seems to update the files, but working with it to get the initial content from the selected file that was already there.