Jump to content

Writing into text files


ted_chou12

Recommended Posts

I have my text files in this format:
test1.test2.test3.test4
test1.test2.test3.test4

and if I just want to edit a small part of it, say test2 of the second line, and store the rest back into the text file, how would I do that?
I have got a function already:
[code]
function explode_lines($filename)
{$lines = file($filename);
foreach($lines as $num => $line)
$lines[$num] = explode(".", $line);
return $lines;}
[/code]
but this only calls out the specified data, what I want to do next would be replacing the old data with the new data where the line number and the explode number is specified.
Reply to this if you dont understand.
Thanks
Ted
Link to comment
Share on other sites

I am a bit stuck, this is what I have so far:
[code]
<?php
$photoarray = file("storage/$username/photo.txt");//this gets the file contents as an array
foreach($photoarray as $photoarray){
$photoarray[] = explode_lines("storage/$username/photo.txt");}
$file = fopen("storage/$username/photo.txt","w+");
$write = fwrite($file,"$photourl;seDp#$title;seDp#$message;seDp#$datetime");//these variables are what i want to write into the text files. ;seDp# is my dividing element
foreach ($photoarray as $photoarray) {fwrite( $file, "$photoarray");}
fclose($file);
?>[/code]

I dont think this is right at all, can someone guide me throught this please?
Thanks
Ted
Link to comment
Share on other sites

[code]
<?php
// open file
$handle = fopen("text1.txt", "r+b");

// get contents of file, line by line, and put into an array
while (!feof($handle)) {
  $list[] = fgets($handle, 4096);
} // end while

// assuming line nodes are sperated by dots..
// explode the 2nd line of the file
$nodes = explode('.',$list[1]);
// change the 2nd node to something else
$nodes[1] = 'something';
// make it back to a string put it back into the list
$list[1] = implode('.',$nodes);

// put the pointer back to the beginning
rewind($handle);

// for each line...
foreach ($list as $val) {
  // write the line to the file
  fwrite($handle, $val);
} // end foreach

// close file
fclose($handle);

?>
[/code]
Link to comment
Share on other sites

yeap, it works, but when I try deleting the whole line:
// open file
$handle = fopen("storage/$username/photo.txt", "r+b");

// get contents of file, line by line, and put into an array
while (!feof($handle)) {
$list[] = fgets($handle, 4096);
} // end while
$list[$id] = "";//this is what I had changed, and $id is set previously to the line number I want to edit.

// put the pointer back to the beginning
rewind($handle);

// for each line...
foreach ($list as $val) {
// write the line to the file
$write = fwrite($handle, $val);
} // end foreach

// close file
close($handle);

It doesnt work, it changes the specified line to the last line of the text...
Ted
Link to comment
Share on other sites

[code]
<?php $id = $_GET['id'];

if(isset($_POST['delete'])){
// open file
$handle = fopen("storage/$username/photo.txt", "r+b");

// get contents of file, line by line, and put into an array
while (!feof($handle)) {
  $list[] = fgets($handle, 4096);
} // end while
$list[$id] = "";//here is where i want you to have a look at, have i done it correctly?

// put the pointer back to the beginning
rewind($handle);

// for each line...
foreach ($list as $val) {
  // write the line to the file
$write = fwrite($handle, $val);
} // end foreach

// close file
fclose($handle);

if($write == false)
{echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";}
else
{header("location: confirm.php?confirm=photoedit_delete");}}?>
[/code]
this is the relevant part.
THanks
Ted
Link to comment
Share on other sites

the code you have provided looks fine, so the only thing I can think of is that  $id is not holding the number you are expecting it to hold. Yes I see that you've properly retrieved the GET. Yes I understand you know that if it is 0 then it is the first line. But where is the code that passes it to begin with? did you actually echo $id and even $list[$id] to see if it has what you are expecting it to have? What does your flatfile look like? Does it actually contain the info you expect it to have? Are you pointing to the right file?

or..maybe you should be more specific as to what is wrong.
Link to comment
Share on other sites

I have used this in the past up to you if you want to use it
[code]<?php
    // open file
    $file = "storage/$username/photo.txt";
    // get contents of file, line by line, and put into an array
    $data = file($file);
    $data[$id] = NULL;
    // Open file for writing
    $handle = fopen($file, "w");
// for each line...
foreach ($data as $val) {
        // write the line to the file
        $write = fwrite($handle, $val);
} // end foreach
        // close file
fclose($handle);
?>[/code]

Ray
Link to comment
Share on other sites

yeap, the code seems to work fine, below is my page:
[code]

<?php $id = $_GET['id'];//get the row from the url, so the page knows which line to edit.

//if submitted by the form, then take this action.
if(isset($_POST['delete'])){
// open file
$file = "storage/$username/photo.txt";
// get contents of file, line by line, and put into an array
$data = file($file);
$data[$id] = NULL;
// Open file for writing
$handle = fopen($file, "w");
// for each line...
foreach ($data as $val) {
// write the line to the file
$write = fwrite($handle, $val);
} // end foreach
// close file
fclose($handle);

if($write == false)
{echo"<p><b>Sorry, the database is temporary down, please come back later!</b></p>";}
else
{header("location: confirm.php?confirm=photoedit_delete");}}

//getting the information from the text file into the form boxes.
function explode_lines($filename)
{$lines = file($filename);
foreach($lines as $num => $line)
$lines[$num] = explode(";seDp#", $line);
return $lines;}
$data = explode_lines("storage/$username/photo.txt");

$title = $data[$id][1];
$message = $data[$id][2];

//the form?>
<label>Title</label>
<input name="title" value="<?echo $title?>" type="text" size="30" />
<label>Comments</label>
<textarea name="message" rows="5" cols="5"><?echo $message?></textarea>
<br />
<input class="button" name="submit" type="submit" />
<input class="button" name="delete" type="submit" value="Delete Entry" onClick="javascript: return checkDelete()" />
[/code]
So I am sure that I got my information correctly.
craygo's script does work for nearly all the lines in the text. And when the text line is deleted. It leaves a blank line like:
[code]
1;seDp#title;seDp#content;seDp#time
2;seDp#title;seDp#content;seDp#time
3;seDp#title;seDp#content;seDp#time

[/code]
The last line is left blank. I dont know if this is the cause for the problem.
The problem now is that when the page tries to delete the last line got from the url, instead of redirecting to the confirm page, it stays on the same page, which is this page, and echos the error message: Sorry, the database is temporary down, please come back later!
Despite the last line does get deleted when preforming the action. (ie. it does delete the last line, but it doesnt perform like it should be, that is going to the confirm page, instead, it remains on this page and echoing the error message.)
I know why it echos the error message, because when I want to delete line 4, which is the last line of the text now, the url will be id=3, and after the submit button for delete is pushed, it deletes line 4, but remains on the url id=3, therefore, since there is no line 4 now, it echoes the error message. But what I dont understand is why doesnt it redirect to the confirm page? All other lines work, but just the last line of every text does not work.
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.