0

I would like to understand how to save my output text file to a directory called feeds.

file_put_contents(date('Y-m-d_His') . './/feeds//.archive.txt', $output);
file_put_contents(date('Y-m-d_His') . '.\feeds\.archive.txt', $output);

There are no errors just don't understand how to tell the output file to save to a directory called feeds.

M.Hemant
  • 2,345
  • 1
  • 9
  • 14
  • possible duplicate of permission issue please refer this link [https://stackoverflow.com/questions/4577149/php-file-put-contents-function-not-working] – M.Hemant May 24 '19 at 04:17
  • All that is good I'm afraid. –  May 24 '19 at 04:21
  • One more thing why there is a `date('Y-m-d_His')` in the first `parameter` it must be static file path with name – M.Hemant May 24 '19 at 04:22
  • you can see here. https://stackoverflow.com/questions/9265274/php-create-and-save-a-txt-file-to-root-directory – PHP Geek May 24 '19 at 04:31

4 Answers4

0

Remove date('Y-m-d_His') from filepath

file_put_contents('feeds/archive.txt', $output);

OR If you want to join date_time with file name then

file_put_contents('feeds/archive'.date('Y-m-d_His').'.txt', $output);

Keep in mind, this file name is unique now so whenever you want to get/read this file, you need to give same file name to read it

M.Hemant
  • 2,345
  • 1
  • 9
  • 14
  • Can I still join the date to the file name this way? –  May 24 '19 at 11:50
  • yes, you can concate date_time in file name if you want like `'feeds/archive'.date('Y-m-d_His')` – M.Hemant May 24 '19 at 11:53
  • Ok I have this but somehow missing the .txt extension. file_put_contents('feeds/feed'. '-'. date('Y-m-d_His').txt, $output); –  May 24 '19 at 12:08
  • Thank you this helps get everything into the same place. What I would be doing now is reading the files that are placed in the feeds folder and building one txt file for reading (done that). While this is not a great option as it grows, I need to figure out a way to perhaps only show a little data at a time. I was thinking of rather than combining all the txt into one just read what is there in "feeds" in blocks of 20 perhaps based on news date range? –  May 24 '19 at 12:20
0

In its most basic form, file_put_contents takes the following parameters:

file_put_contents ( string $filename , mixed $data )

The call you are making is interpreting the date as a path, './/feeds//.archive.txt' as the data to write, and $output as a context resource.

0

you can create file in the same directory.

$date_to_save= date('Y-m-d_His');
$fileOpen = fopen($_SERVER['DOCUMENT_ROOT'] . "/archive.txt","wb");
fwrite($fileOpen ,$date_to_save);<--writes
fclose($fileOpen );<---close
Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
0

ob_start(); <--data to capture here-->

$output = ob_get_contents(); ob_end_flush();

file_put_contents('feeds/stream'. '-' .date('Y-m-d_His').'.txt', $output);

The final result - thank you