How to write into a file in PHP?

PhpFile

Php Problem Overview


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

<html>
<body>

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


fclose($file);
?>

</body>
</html>

It creates the file lidn.txt, but it's empty.

How can I create a file and write something into it, for example the line "Cats chase mice"?

Php Solutions


Solution 1 - Php

You can use a higher-level function like:

file_put_contents($filename, $content);

which is identical to calling fopen(), fwrite(), and fclose() successively to write data to a file.

Docs: file_put_contents

Solution 2 - Php

Consider fwrite():

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

Solution 3 - Php

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

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

Solution 4 - Php

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

You use fwrite()

Solution 5 - Php

It is easy to write file :

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

Solution 6 - Php

I use the following code to write files on my web directory.

write_file.html

<form action="file.php"method="post">
<textarea name="code">Code goes here</textarea>
<input type="submit"value="submit">
</form>

write_file.php

<?php
// strip slashes before putting the form data into target file
$cd = stripslashes($_POST['code']);

// Show the msg, if the code string is empty
if (empty($cd))
    echo "Nothing to write";

// if the code string is not empty then open the target file and put form data in it
else
{
    $file = fopen("demo.php", "w");
    echo fwrite($file, $cd);

    // show a success msg 
    echo "data successfully entered";
    fclose($file);
}
?>

This is a working script. be sure to change the url in the form action and the target file in fopen() function if you want to use it on your site.

Solution 7 - Php

Here are the steps:

  1. Open the file

  2. Write to the file

  3. Close the file

     $select = "data what we trying to store in a file";
     $file = fopen("/var/www/htdocs/folder/test.txt", "w");        
     fwrite($file, $select->__toString());
     fclose($file);
    

Solution 8 - Php

In order to write to a file in PHP you need to go through the following steps:

  1. Open the file

  2. Write to the file

  3. Close the file

     $select = "data what we trying to store in a file";
     $file = fopen("/var/www/htdocs/folder/test.txt", "a");
     fwrite($file  , $select->__toString());
    		 fclose($file );
    

Solution 9 - Php

fwrite() is a smidgen faster and file_put_contents() is just a wrapper around those three methods anyway, so you would lose the overhead. Article

>file_put_contents(file,data,mode,context):

The file_put_contents writes a string to a file.

This function follows these rules when accessing a file.If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename Create the file if it does not exist then Open the file and Lock the file if LOCK_EX is set and If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content Write the data into the file and Close the file and release any locks. This function returns the number of the character written into the file on success, or FALSE on failure.

>fwrite(file,string,length):

The fwrite writes to an open file.The function will stop at the end of the file or when it reaches the specified length, whichever comes first.This function returns the number of bytes written or FALSE on failure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrilliantView Question on Stackoverflow
Solution 1 - PhpSavagemanView Answer on Stackoverflow
Solution 2 - PhpsathishView Answer on Stackoverflow
Solution 3 - PhpPesseView Answer on Stackoverflow
Solution 4 - PhplemonView Answer on Stackoverflow
Solution 5 - PhpTrần Khải HoàngView Answer on Stackoverflow
Solution 6 - PhpAmit VermaView Answer on Stackoverflow
Solution 7 - Phpuser10603371View Answer on Stackoverflow
Solution 8 - Phpmallikarjun S.K.View Answer on Stackoverflow
Solution 9 - PhpA.D.View Answer on Stackoverflow