How to Write into a File in PHP

How to write into a file in 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

Create or write/append in text file

Try something like this:

 $txt = "user id date";
$myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);

how to write into a text file in php without delete all the data

Note: This will only work when you have appropriate permission for test.txt else it will say

permission denied (un-appropriate will lead to this)

Here we are using:

1. a which is for append this will append text at the end of file.

2. instead of w, flag w is for write, which will write on file without caring about you existing data in that file.

PHP code:

<?php
ini_set('display_errors', 1);
$txt = 'srge';
$file = fopen('text.txt','a');
fwrite($file,$txt);

create, write and download a txt file using php

The correct way to do it would be:

<?php

$file = "test.txt";
$txt = fopen($file, "w") or die("Unable to open file!");
fwrite($txt, "lorem ipsum");
fclose($txt);

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: text/plain");
readfile($file);

?>

PHP to insert into file

Try this:

<?php
$test = "http://www.onlinedealsindia.in/deal/SOME-UNIQUE-LINK";
$file = ( $_SERVER['DOCUMENT_ROOT'] . '/sitemap.txt' );
$contents = explode("\n", file_get_contents($file), 6);
file_put_contents($file, $contents[0]."\n".$contents[1]."\n".$contents[2]."\n".$contents[3]."\n".$contents[4]."\n".$test."\n".$contents[5]);
?>

How to write a structure into a file in PHP?

You could write your structure to a file with file_put_contents as you have outlined in your post.

You can serialize and unserialize the arrays or objects into the file using the following documentation:

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

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

So it would look like this:

file_put_contents($fileName, serialize($MyStructure), FILE_APPEND);

How to write a text file line by line in PHP?

Just change your code:

if( isset($_POST['submit']) ){
$name = $_POST['name'];
$age = $_POST['age'];
$address = $_POST['address'];
$file = fopen("student.txt","a");
fwrite($file,$name.PHP_EOL);
fwrite($file,$age.PHP_EOL);
fwrite($file,$address.PHP_EOL);
fclose($file);
echo "Adding student successfully";
}

I hope it works for you. :)

Not able to write php file in php

may be a ownership problem. try do chmod -R 775 /var/www/folder and/or chown -R domain:www-data /var/www/folder - where domain is the user of that particular virtualhost or www-data

Also to update a file instead rewrite you can use append option - adding + will help to create the file first time if it is not exists.

$file = fopen("test.php","a+");
echo fwrite($file,"do not vote me down ab");
fclose($file);

And If you want to change the ownership of the file you can use Php chown function-

chown('test.php','apache');

Too change permission of file or folder recusively use this code -

exec ("find /path/to/folder -type d -exec chmod 0750 {} +");
exec ("find /path/to/folder -type f -exec chmod 0644 {} +");

In PHP, why won't this write to test.txt?

Write this in a console

chmod a+w test.txt 

Wordpress can't write file

You need to be using your absolute path or DOCUMENT path .. What you have right now, is trying to look for a directory called output in the root of the filesystem or root of the application where the actual call is made, so if you're inside a class directory when the call is made, it's going to be looking in THAT directory instead of the one the script is in.

<?php                                                                                                  
$fl = fopen ( "/var/www/path/to/your/wp/output/test.txt" , "w" );
fwrite ( $fl , "This is a test" );
fclose ( $fl );
?>

OR

<?php                                                                                                  
$fl = fopen ( $_SERVER["DOCUMENT_ROOT"] . "/myWPFolder/output/test.txt" , "w" );
fwrite ( $fl , "This is a test" );
fclose ( $fl );
?>


Related Topics



Leave a reply



Submit