JDT

 

FREE Downloads
Articles
Tutorials

 

Using PHP to Write the Contents of a Variable to a File


It is sometimes necessary to write the contents of a variable to a file. Here is a simple way to do it in PHP.

Assume that the variable '$contents' contains some text, for example, "hello world". If you want to write this content to a file called 'contentsFile.txt, which is located in the 'webpages' folder on your web server, you could use the following bit of code:

$contentsFile="webpages/contentsFile.txt";
$file = fopen ($contentsFile, "w");
fwrite ($file, $contents);
fclose ($file);

...and that's it!

One possible problem is that by default you will probably not be able to write to files on your web server without changing the permissions on the folder where you want to write the file.

For information on how to resolve this issue, go to Using PHP to Change the Permissions on a Folder.







Go back to PHP Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting / bookkeeping software tool.

Aimed at contractors and freelancers, Earnings Tracker enables you to perform many bookkeeping and accounting tasks, helping you to keep track of your company's earnings and outgoings.

The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.

As it is written in open source software, you are free to modify the code yourself, enabling you to produce a customized version of Earnings Tracker that fits your own specification.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

Need free accounting software
 


JDT

Copyright Notice for John Dixon Technology Ltd

Privacy Statement

Terms & Conditions