user2738843
user2738843

Reputation: 75

PHP not writing to file

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);

Upvotes: 0

Views: 1043

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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);
}

Upvotes: 1

user2734435
user2734435

Reputation: 95

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.

Upvotes: 0

Related Questions