tags:

views:

123

answers:

5

Hello everybody!!!

I have this script on one free PHP-suppoting server:

<html>
<body>

<?php
$file = fopen("lidn.txt","a");


fclose($file);
?>

</body>
</html>

it does create the lidn.txt file, but it's empty.

I want to create a file and to write something into it, for example this line "Cats chase mice", how can I do it?

+2  A: 
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase');
fwrite($fp, 'mice');
fclose($fp);

http://php.net/manual/en/function.fwrite.php

Pesse
+5  A: 

Use fwrite()

<?php
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
RSK
Thank you very much!!!
brilliant
+1  A: 
$text = "Cats chase mice";
$filename = "somefile.txt";
$fh = fopen($filename, "a");
fwrite($fh, $text);
fclose($fh);

You use fwrite()

lemon
+2  A: 

It is easy to write file :

$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
Trần Khải Hoàng
+2  A: 

You can also sure higer-level function like file_put_contents($filename, $content) which does a really good job!

Savageman
WOW!!! Thank you!
brilliant
Just a sidenote, `file_put_contents()` works in PHP5 only. Doesn't seem like a problem in this case (your answer got accepter, after all), but there might still be a few hosts out there running PHP4.x .
Duroth