|
|
|
|
![]() ![]() |
May 12 2005, 05:56 AM
Post
#1
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 2 Joined: 12-May 05 From: Christchurch, New Zealand Member No.: 5,002 |
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 |
|
|
|
May 12 2005, 06:16 AM
Post
#2
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 20 Joined: 6-May 05 Member No.: 4,780 |
Before you can write data into a file this should be existed:
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. |
|
|
|
May 14 2005, 03:30 AM
Post
#3
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 2 Joined: 12-May 05 From: Christchurch, New Zealand Member No.: 5,002 |
Thats great thanks
|
|
|
|
May 15 2005, 02:03 AM
Post
#4
|
|
|
Newbie [ Level 1 ] Group: Banned Posts: 6 Joined: 15-May 05 Member No.: 5,083 |
|
|
|
|
Mar 25 2008, 10:12 PM
Post
#5
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 22 Joined: 25-March 08 Member No.: 29,382 |
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
|
Lo-Fi Version | Time is now: 7th July 2008 - 03:45 PM |