8

trying to add strings in a file on the same line using Storage:: disk('local')->append() method but in the output file I'm getting each string on a new line

what i did is:

$string = "first-"; 
Storage::disk('local')->append('filename', $string);
$string = "second"; 
Storage::disk('local')->append('filename', $string);

and the output file was

first-
second

but I'm trying to get the file without line breaks like

first-second
5
  • Not sure if there's a built-in method to do that, but you could try a longer approach, get the contents of the file, like $contents = file_get_contents(...), append in PHP, via $contents .= 'second';, then put the whole string into the file. (or something similar) Commented Jan 16, 2020 at 16:00
  • the file will be a big file and don't want to open it and loaded in the memory Commented Jan 16, 2020 at 16:02
  • Yeah, fair point. You can see all the methods available for the FileSystem class (extension of Storage::disk() facade method) here: laravel.com/api/5.8/Illuminate/Contracts/Filesystem/…. At a glance, I don't see anything, but do some digging, might find something that works. Commented Jan 16, 2020 at 16:06
  • stackoverflow.com/questions/54659226/… check that answer. unfortunately append reads whole data Commented Jan 16, 2020 at 16:19
  • after testing, i ended up using the php "file_put_contents" function, it's doing the job and without filling the memory Commented Feb 4, 2020 at 11:27

1 Answer 1

22

append() using FilesystemAdapter

public function append($path, $data, $separator = PHP_EOL)

PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way.

So, you can use :

Storage::disk('local')->append('filename', $string, null);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.