Showing posts with label PHP FILE_PUT_CONTENTS. Show all posts
Showing posts with label PHP FILE_PUT_CONTENTS. Show all posts

Tuesday, 11 September 2018

PHP: Write and append to files with file_put_contents

The PHP file_put_contents() function is a useful shortcut for dumping a string into a file (compared with using fopen, fputs etc). By default it will create a new file if it does not exist, or overwrite the file if it does exist. Recently I re-read the documentation for this function and discovered it is also possible to append to a file with file_put_contents.

Writing to a new file / overwriting an existing file

Pass a filename and the string to write to the file (an array containing a stream can also be passed but that's beyond the scope of this post) and either a new file will be created containing the contents of the string or an existing one will be overwritten with the contents of the string.
file_put_contents('/path/to/filename', $data);
Note that the directory you wish to write the file to must exist and be writable to as the user the script is running as. If overwriting an existing file then that file must be writable as the user the script is running as.

Appending to an existing file

As I discovered recently, it is also possible to append to an existing file using file_put_contents. The function can take a third parameter containing a list of flags joined with a binary OR operator.
To append to a file, pass the FILE_APPEND flag like so:
file_put_contents('/path/to/filename', $data, FILE_APPEND);
As with overwriting an existing file, the file must be writable as the user the script is running as.

Related posts:

Thursday, 30 August 2018

delete a new line from the txt file after inserting the value

I create a txt file from a folder in that way:

$fp = fopen("mylist.txt","rw+");
foreach(glob("folder/*.*") as $value){
fwrite($fp,$value."\n");
}

and it create inside the txt each items, one per line, BUT it also add a newline at the end of the file.
This is the content of mylist.txt:
foldelr/file1.mp3
folder/file2.mp3
(BLANK NEWLINE)

I tried to remove the blank newline at the end of the txt file, in this way:
$filetxt = fopen("mylist.txt","r");
$rtrim = rtrim($filetxt, "\n");
$newfile = file_put_contents("newfile.txt", $rtrim);

But it doesn't work, because in the "newfile.txt", i still have the newline blank after last file name.
I tried to convert file in array, use "array_diff()" to remove the last line, but it fail. I tried also all the suggestions in this thread: remove new line characters from txt file using php but not fix my problem.
Does anyone can please help me to understand what i'm missing or mistaken?
Kind Regards
Brus

Use file_get_contents method it works fine for me
    $fp = fopen("mylist.txt","rw+");
    foreach (glob("test/*.*") as $value) {
        fwrite($fp, $value . "\n");
    }

    $filetxt = file_get_contents("mylist.txt");
    $rtrim =  rtrim($filetxt, "\n");
    $newfile = file_put_contents("mylist.txt", $rtrim);

Or You can also remove the last newline by using the array count.
$fp = fopen("mylist.txt","rw+");
$i =0;
foreach(glob("folder/*.*") as $value){
  $i++;
  if($i < count(glob("folder/*.*")))
    fwrite($fp,$value."\n");
  else
    fwrite($fp,$value);
}