-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Filesystem] Add appendToFile() #20612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
3.3.0 | ||
----- | ||
|
||
* added `appendToFile()` to append contents to existing files | ||
|
||
3.2.0 | ||
----- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -624,7 +624,7 @@ public function tempnam($dir, $prefix) | |
* @param string $filename The file to be written to | ||
* @param string $content The data to write into the file | ||
* | ||
* @throws IOException If the file cannot be written to. | ||
* @throws IOException If the file cannot be written to | ||
*/ | ||
public function dumpFile($filename, $content) | ||
{ | ||
|
@@ -648,6 +648,31 @@ public function dumpFile($filename, $content) | |
$this->rename($tmpFile, $filename, true); | ||
} | ||
|
||
/** | ||
* Appends content to an existing file. | ||
* | ||
* @param string $filename The file to which to append content | ||
* @param string $content The content to append | ||
* | ||
* @throws IOException If the file is not writable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you remove the capital letter above, you should do the same here, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverted the removal of the capital letter |
||
*/ | ||
public function appendToFile($filename, $content) | ||
{ | ||
$dir = dirname($filename); | ||
|
||
if (!is_dir($dir)) { | ||
$this->mkdir($dir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we have to check here too that the directory is writable after we created it (the directory might have been created by another process)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, done. The same could be applied on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think you are right. Should be done on |
||
} | ||
|
||
if (!is_writable($dir)) { | ||
throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir); | ||
} | ||
|
||
if (false === @file_put_contents($filename, $content, FILE_APPEND)) { | ||
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $files | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For using
file_put_contents
we technically allowstring|array|resource
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, updated on the two newly added methods.
Since this targets master, would you mind to open a PR against 2.8 (it doesn't handle streams in 2.7, see 0fc513e) for changing this docblock? I think it's worth it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree.