0

Ok so I have this thing setup to write things to text, but it will not actually write the txt to the file. It deletes the file then creates it again with the data inside.

$_POST['IP']=$ip;
unlink('boot_ip.txt');
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt","wb");
fwrite($fp,$IP) ;   
fclose($fp);
5
  • 1
    It deletes the file because you use unlink
    – Rob
    Commented Sep 2, 2013 at 6:51
  • Also what mode is "wb" ? You might be looking for "a" instead
    – Dale
    Commented Sep 2, 2013 at 6:52
  • 1
    something to consider is you have $ip !== $IP and you shouldn't use $_POST['something'] = $something
    – Class
    Commented Sep 2, 2013 at 6:54
  • 1
    @Dale: in this case, 'wb' stands for "write binary" (think FTP transfer modes, binary vs text).
    – user1233508
    Commented Sep 2, 2013 at 6:55
  • @DCoder Yes sorry, I am aware of that mode but it appears he wants it to append to the file (hard to tell from the question :)
    – Dale
    Commented Sep 2, 2013 at 6:56

2 Answers 2

1

Your variables were not properly set and were done the other way around.

Quick note: wb means to write binary. Unless that is not your intention, I suggest you use only w.

Your filename ending in .txt is text, therefore use the w switch. That will overwrite previous content.

You had:

$_POST['IP']=$ip;
unlink('boot_ip.txt');
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt","wb");
fwrite($fp,$IP);   
fclose($fp);

This => $_POST['IP']=$ip; where it should be $ip=$_POST['IP'];

and this fwrite($fp,$IP); should be fwrite($fp,$ip);

You had the $IP in uppercase when it should be in lowercase as you declared in your variable.

NOTE: The unlink part of the code may need to reflect your folder's place on your server.

However, I suggest you do not use unlink because using it will throw an error right away, because the file may not be found to begin with, since it would have already been unlinked.

You can either not use it, or use an if statement. See my example following my code below.

Plus, using the w switch, will automatically overwrite previously written content.

If you need to append/add to the file, then you will need to use the a or a+ switch.

If that is the case, then you will need to use the following:

$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt","a");
fwrite($fp,$ip . "\n");

Reformatted (tested and working)

$ip=$_POST['IP'];
unlink('boot_ip.txt');
// use the one below here
// unlink($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt");
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt","wb");
fwrite($fp,$ip);   
fclose($fp);

Using the following form:

<form action="handler.php" method="post">

<input type="text" name="IP">

<input type="submit" value="Submit">

</form>

Using an if statement method.

$ip=$_POST['IP'];
    if(!file_exists($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt")) {

$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/boot/boot_ip.txt","wb");
fwrite($fp,$ip);   
fclose($fp);
}
0

Traditionally, That's exactly how text files work. It's a sequential access file rather than a random access file. Everything needs to be re written every time you add new information to a file. That's why it's slow and inefficient for large scale projects.

There's no way around it. Either read the data from the file, and re-write it with the new information, or make a random access file. That's how it's taught in most languages and in classrooms. It's mostly so you understand the processes.

In practice though if you're only appending data to the end:

unlink(); in php deletes a file, so you don't need it.

ALSO

see: http://www.w3schools.com/php/php_file.asp

for how to write to a file and the parameters you can use for behaviour

specifically look at the parameters for write modes: r, w, rw+, etc....

a is probably the one you want.

It still re-creates the file like I said, but does all the reading and rewriting for you so you don't have to do it yourself.

the parameter you entered "wb" DOES contain a w. so i assume a part of it is the same as simply "w" which, like i said earlier, would clear the file if it exists before writing new data.

My solution for you is aka, TL;DR version:

$fp=fopen("boot_ip.txt","a");

(I didn't use the full form like you did, but the import change is the second parameter a rather than wb) and exclusion of unlink(); )

then do your writes. This should add new data to the end of the file.

6
  • Will using $fp=fopen("boot_ip.txt","w"); replace the text in there? Commented Sep 2, 2013 at 7:16
  • @user2738843 Yes. Read the documentation, it explains what all the mode values do.
    – Barmar
    Commented Sep 2, 2013 at 7:23
  • @user2738843 yes it will. So if you use "w" you will lose all the data before writing into the file. if you use "a" instead, then it will do the reading and writing for you, and just add the new data to the end. the last option is w+ which is read and write. in which case. Read all your data, store it, and write it back into the file. Commented Sep 2, 2013 at 7:24
  • please upvote and/or accept answer if I have helped. That way I can help others better by being able to comment and ask for clarification before i provide an answer (Which i'm one vote away from being able to do right now) thanks. Commented Sep 2, 2013 at 7:32
  • @user2738843 did you take out the unlink(); line from your code as well? Commented Sep 2, 2013 at 8:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.