PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do I open a file to write content to?


Working with files and folders in PHP is fairly easy though it can take a little while to get used to.

You need to create a filehandle, which is a pointer to a file, that you wish to write too.

Use the fopen function to open a file, and pass the name of the file as the first parameter and what you want to do with it second - in this case 'w' for write.

Then simple use fwrite to write to your file, and close it with fclose.

Here's a simple example that will create a file in the directory called example.txt and write to it with just a few lines of PHP code.

<?php

$myfile 
fopen("example.txt","w");
fwrite($myfile,"That was easy!");
fclose($myfile);
?>


Congratulations, you have just written text to a file using PHP!

If you get an error message doing this on a web server, make sure you have the relevant permissions to write to the directory you are running the script in.



ASK A QUESTION

More files PHP Questions

How can I export data into Word/Excel documents?

What is the use of file in php?

Would like to create a web page by using the contents of file#1 as input, writing contents of a variable as content, and ending with contents of file#2. Would you please help me learn how to do this? Here is my attempt: $in1 = 'input1.html'; $in2 =

How do I compare strings in two file and return similar string in another file?

I am trying to find the most recent directory that I am working in. example http://www.mysite.com/apples/index.php how do I get php to find the directory "apples" so I can make it a directory



Custom Search