|
|
How Do I Create And Write To Files? - creating, writing, deleting files | ||
Discussion by alfonzo with 4 Replies.
Last Update: March 25, 2008, 10:12 pm | |||
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
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
Thu May 12, 2005 Reply New Discussion
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:
$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.
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.
Thu May 12, 2005 Reply New Discussion
Thats great thanks
Sat May 14, 2005 Reply New Discussion
Sun May 15, 2005 Reply New Discussion
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 );
?>
Tue Mar 25, 2008 Reply New Discussion
How To Check Email Adress And Name Validity? Refusing wrong info in a form... (12)
|
(16) Online HTML/PHP Editor: Edit File In Browser!
|
Index




