| | Hi, Can someone please tell me how to create files and write to them in PHP. I just want to create a simple file containing text, and then be able to read it or update it. Thanks Alfie |
| Nov 22, 2009 |
Here is a sample code on how to read/write into a file. Create a new php file then type this: CODE <?php $myFile = "test.txt"; $filehandler = fopen($myFile, 'w') or die("can't open file"); $stringData = "You're text here \r"; fwrite($filehandler, $stringData); $stringData = "Another Text here \r"; fwrite($filehandler, $stringData); fclose($filehandler ); ?> Explanation: Var $myFile on the first line declares the filename of you're file.If its on different directory please specify that directory where it is located. The second line creates a variablle that will handle the file..That is why i called this file handler. fopen() is a function that will open that file. 'w' is for you to write..If you only want to read it just make it 'r' $stringData variable is the string or character you want to write on the file plus a carriage return. The fourth line will actually do the writing. fwrite() function.It takes two parameter the filehandler and the string you will write which is $stringData. And everytime you open a file you will also need to close this using fclose() function. Hope it helps.
CODE <?php $myFile = "test.txt"; $filehandler = fopen($myFile, 'w') or die("can't open file"); // this will create a new file if the file does not exist other wise overwrite it // to ensure that you dont overwrite an existing file but append to it use fopen($myFile, 'a') // if you want to read and write to the same file use w+/a+ mode to open the file $stringData = "You're text here \r"; fwrite($filehandler, $stringData); $stringData = "Another Text here \r"; fwrite($filehandler, $stringData); fclose($filehandler ); ?>
Similar Topics
Keywords : Files Creating Writing Deleting Files
It recently came upon me that I was designing a website that used file uploads. Of course, PHP was Today, I came up with this strange PHP behaviour. Just wanted to know if anyone has any Hey! How can i make my included files 100% safe. Like if I include a file witht his code.. CODE i have this error QUOTE Warning: session_start() : Cannot send session cache limiter - headers I am in the process of writing a small content management system for my niche sites and need to be Right i have done a check for a tutorial on this as well as a question about it but php is not It turns out that the authentication script that I copied from I want to transfer files from one folder to another or an entire folder in my web space. I don't Hi, You can use frontpage to preview HTML files, but how can I edit and preview PHP files offline. I am doing pre planning for the blenderxgrid.com script. I was originally going to do this with Dear Friends I need solution to a problem. The problem is as under: I am creating certain files I've recently been trying to read up on using PHP and GD to create PNG's on the fly. It is posible for PHP to delete files on a server? If so, how is this possible? Just out of this code to Read from file you can use this code in to make a small data base and it is very to Hello. A while ago, a friend showed me this PHP code (wget) which allows you to get files from other This is good for those who doesnt have knowledge bout PHP they are totally free here are my 2 know Looking for create, write, files, creating, writing, deleting, files
|
![]() How Do I Create And Write To Files? - creating, writing, deleting files |
Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com