File_put_content

Started on the old forum in January 2013 · view the original in the archive · old links redirect here

2 replies · 2,116 views · started 13 years ago · 1 min read

3 participants
Surya4u Junior · 29 posts #1

I want Save "data.txt" File In Folder "/data" as name 'datax.txt'

Is It possible With Below Code?

$file='data.txt'; $folder='/data'; file_put_content($file, $folder);

Ghost Senior · 366 posts #2

not with that code as its full of syntax errors.

but this would work

<?php
$file = 'data/data.txt';
$string = 'A Testing Message Inside A Txt File.'; //Can Be Blank also.
file_put_contents($file, $string);
?>
//
//
//

to append to file this way you would do

<?php
$file = 'data/data.txt';
$string = 'A Testing Message Inside A Txt File.'; //Can Be Blank also.
file_put_contents($file, $string, FILE_APPEND | LOCK_EX); // LOCK_EX would stop anyone else writing to the file at the same time.
?>
//
//
//

although im not sure it actually sets the file permission for you. so you could add to end of that.

chmod($file, 0777);
//
//
//
<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
wap2k Developer · 168 posts #3

file_put_contents function context was only added to PHP at version 5.0.0. This function is just a PHP Wrapper and is actually identical to calling fopen(), fwrite() and fclose() successively to write data to a file. The ability to lock a file while it is being used with the LOCK_EX flag was added in PHP 5.0.1.

Use file_put_contents($file, $data, FILE_APPEND); to append date to the end of a file.

Regards, Rob

Sign in to reply.

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close