PHP bir dosyaya yazmak nasıl?

5 Cevap php

Ben bir ücretsiz PHP-suppoting sunucuda bu komut dosyası var:

<html>
<body>

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


fclose($file);
?>

</body>
</html>

Bu lidn.txt dosyası oluşturmak, ama boş.

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

5 Cevap

Ayrıca file_put_contents($filename, $content) gerçekten iyi bir iş yok gibi bir üst düzey işlevini kullanabilirsiniz!

Fwrite () kullanın

<?php
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase');
fwrite($fp, 'mice');
fclose($fp);

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

Bu dosyayı yazmak kolaydır:

$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
$text = "Cats chase mice";
$filename = "somefile.txt";
$fh = fopen($filename, "a");
fwrite($fh, $text);
fclose($fh);

Kullanmak fwrite()