RoXes
RoXes

Reputation: 49

PHP append content to a file (reading/writing of a file)

I'm still rather new to PHP and I'm working on a trying to do something kinda like this;

down.php?days=numberHere take that number and write it to a file and then display the file in a spot, I know how to display the file but I'm not really sure how to write to the file with GET if someone could point me in a direction to learn or help me out that'd be great, I've looked around for a few days and haven't seen anything that would help

Thanks

Edit:

I've re-read my question and some of the answers, and I don't know if I was really clear in what I'm trying to accomplish; Basically, If Cooldowns.txt has "10" and ?days=7, then cooldowns.txt needs to become 17 instead of adding the numbers to the file


Update: I've come up with something like that based on the replies to my question, I'm wondering if there's a way to make it add the get ? (IE get = 1 + whatever the file already has, so if the file has 25 and get = 1 file becomes 26)

Upvotes: 1

Views: 1097

Answers (3)

daulat
daulat

Reputation: 949

<?php
// Try this code
if(isset($_GET['days'])){
    $number=(int)$_GET['days']."\n";
    $file='file.txt';
/* 
/The existing data in file is preserved.
/File pointer starts at the end of the /file.
/Creates a new file if the file doesn't exist
*/
    $fopen=fopen($file,'a+');
    $fwrite=fwrite($fopen, $number);
    echo readfile($file);
}
?>

Upvotes: 3

Mostafa Talebi
Mostafa Talebi

Reputation: 9183

Use file_put_contents(), read about it here:

file_put_contents("file.txt", "My Content");

(EDITED) If want to append to a file's current content, use FILE_APPEND constant and pass it as third parameter of the function:

file_put_contents("file.txt", "\r\n" . "My Content", FILE_APPEND);

mode Description

'r' Open for reading only; place the file pointer at the beginning of the file.

'r+' Open for reading and writing; place the file pointer at the beginning of the file.

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

'x+' Create and open for reading and writing; otherwise it has the same behavior as 'x'.

'c' Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).

'c+' Open the file for reading and writing; otherwise it has the same behavior as 'c'.

But PHP also allows a lower level of file I/O, by using fopen() function, it returns you a file descriptor which allows various modes of I/O:

$fd = fopen("file.txt");

Read about it here.

You then pass the file descriptor to other f* family functions:

fwrite($fd, "Content");

The $mode parameter of fopen() function helps you selecting how to read/write:

According to the table above taken from fopen() documentation on php.net (link given), you can use mode a:

fopen("file.txt", "a");

Upvotes: 1

Devon Bessemer
Devon Bessemer

Reputation: 35337

From quote:

I've come up with something like that based on the replies to my question, I'm wondering if there's a way to make it add the get ? (IE get = 1 + whatever the file already has, so if the file has 25 and get = 1 file becomes 26)

// Retrieve data from file.txt and type cast it as an integer
$current = (int) file_get_contents('file.txt');
// Increment $current, equivalent to $current=$current+1;
$current++;
// Write back
file_put_contents('file.txt', $current);

Upvotes: 1

Related Questions