note 115952 added to function.file-put-contents
| From: | gurjindersingh at SPAM dot hotmail dot com | Date: | Sat, 18 Oct 2014 18:03:12 +0000 |
| Subject: | note 115952 added to function.file-put-contents | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200678@lists.php.net to get a copy of this message | ||
File put contents fails if you try to put a file in a directory that doesn't exist. This
function creates the directory.
i have updated code of "TrentTompkins at gmail dot com". thanks
<?php
/**
* @param string $filename <p>file name including folder.
* example :: /path/to/file/filename.ext or filename.ext</p>
* @param string $data <p> The data to write.
* </p>
* @param int $flags same flags used for file_put_contents.
* more info: http://php.net/manual/en/function.file-put-contents.php
* @return bool <b>TRUE</b> file created succesfully <br> <b>FALSE</b>
failed to create file.
*/
function file_force_contents($filename, $data, $flags = 0){
if(!is_dir(dirname($filename)))
mkdir(dirname($filename).'/', 0777, TRUE);
return file_put_contents($filename, $data,$flags);
}
// usage
file_force_contents('test1.txt','test1 content'); // test1.txt created
file_force_contents('test2/test2.txt','test2 content');
// test2/test2.txt created "test2" folder.
file_force_contents('~/test3/test3.txt','test3 content');
// /path/to/user/directory/test3/test3.txt created "test3" folder in user directory (check
on linux "ll ~/ | grep test3").
?>
----
Server IP: 72.52.91.14
Probable Submitter: 117.205.68.115
----
Manual Page -- http://php.net/manual/en/function.file-put-contents.php
Edit -- https://master.php.net/note/edit/115952
Del: integrated -- https://master.php.net/note/delete/115952/integrated
Del: useless -- https://master.php.net/note/delete/115952/useless
Del: bad code -- https://master.php.net/note/delete/115952/bad+code
Del: spam -- https://master.php.net/note/delete/115952/spam
Del: non-english -- https://master.php.net/note/delete/115952/non-english
Del: in docs -- https://master.php.net/note/delete/115952/in+docs
Del: other reasons-- https://master.php.net/note/delete/115952
Reject -- https://master.php.net/note/reject/115952
Search -- https://master.php.net/manage/user-notes.php