Welcome Guest ( Log In | Register)



3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> How To Read And Write Files Using Php, php :: reading and writing files
electriic ink
post Aug 26 2005, 10:46 AM
Post #1


Incest is a game the whole family can play.
Group Icon

Group: [MODERATOR]
Posts: 1,217
Joined: 11-February 05
From: Heaven
Member No.: 3,709



How To Read And Write And Files
a simple trick using php

For this script all you need is a php enabled server, a text document and a basic understanding of php itself:

Beginning
  1. Create a text file called name.txt in any directory.
  2. Change the file permissions to 777
  3. Create a empty php file in the same directory.

You are now ready to begin reading and writing your files. If you just want to read the file scroll straight down to Reading the File else read through the whole tut smile.gif


Open The File

Before you can write to the file, you need to open it:

CODE
<?

$thefile = "name.txt"; /* Our filename as defined earlier */
$towrite = "Black widgets rule!"; /* What we'll write to the file */

$openedfile = fopen($thefile, "w");


This opens the file name.txt which we created earlier for writing, but you can open it for reading and both. Just replace the w with one of the following:

QUOTE(php.net)

r -- Open for reading only; place the file pointer at the beginning of the file.
r+ -- Open for reading and writing; place the file pointer at the beginning of the file.
w -- Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
w+ -- Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.


You can find more of these at php.net. I have only quoted a few.

Also, the fopen() function must be stored as a variable!

If you have done what I have said correctly, the file should be opened so now we are ready to write things into it:

Writing The File

Writing to the file is very easy. You use the fwrite() function like so:

CODE
<?

$thefile = "name.txt"; /* Our filename as defined earlier */
$towrite = "Black widgets rule!"; /* What we'll write to the file */

$openedfile = fopen($thefile, "w");
fwrite($openedfile, $towrite);


Again, a breeze all it does is write $towrite to $thefile. You must always write the content to the variable with fopen() stored within it not the one just containing the path to the file! Please bare in mind that because of the way we opened the file all information previously stored in the file will be overwritten!

Note: I was told that you can use file_put_contents() instead of fwrite(). You use it in the same way as fwrite() except you would use $thefile instead of $openedfile and like file_get_contents() there's no fopen() and fclose()! Only for PHP5+ though.

Reading The File

Reading a file is even easier. You could go through the fopen() method but I'm gonna show you an easier way:

CODE
<?
$thefile = "name.txt"; /* Our filename as defined earlier */
$towrite = "Black widgets rule!"; /* What we'll write to the file */

$whatsinthefile = file_get_contents($thefile); ?>


No opening the file, no closing the file. Just that.

Closing the file

If you opened the file using fopen() then you have to close it using fclose(). Simple:

CODE
<?
$thefile = "name.txt"; /* Our filename as defined earlier */
$towrite = "Black widgets rule!"; /* What we'll write to the file */

$openedfile = fopen($thefile, "w");
fwrite($openedfile, $towrite);
fclose($openedfile);
?>


It couldn't be easier than that and if everything is done just as I say there should be no php errors at all smile.gif

Any questions about this tutorial?
Go to the top of the page
 
+Quote Post
wariorpk
post Aug 26 2005, 11:33 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 661
Joined: 18-April 05
Member No.: 5,852



That is a nice tutorial you wrote here. If I ever learn php (not likely I am way too lazy) I would look here. Keep up the good work.
Go to the top of the page
 
+Quote Post
neokid
post Aug 27 2005, 01:48 AM
Post #3


Newbie
*

Group: Members
Posts: 9
Joined: 27-August 05
Member No.: 11,177



OMG thank you, i had alot of trouble but im a beginner
Go to the top of the page
 
+Quote Post
shadowdemon
post Sep 17 2005, 11:31 AM
Post #4


Super Member
*********

Group: Members
Posts: 210
Joined: 13-September 05
Member No.: 11,799



were do u write in it
Go to the top of the page
 
+Quote Post
thablkpanda
post Sep 17 2005, 04:19 PM
Post #5


Super Member
*********

Group: Members
Posts: 339
Joined: 2-December 04
From: Atlanta, GA
Member No.: 2,516



Nice tut, simple though, I'd expect adding by-line writing and such..

I'd also think you don't want to add '?>' to the end of the previous code boxes, but it's not there...

Panda - oh, maybe because it's not the end of the file yet- lol
Go to the top of the page
 
+Quote Post
Amezis
post Sep 17 2005, 04:27 PM
Post #6


Privileged Member
*********

Group: Members
Posts: 535
Joined: 14-February 05
From: Oslo, Norway
Member No.: 3,759



Great tutorial biggrin.gif

I'm only a newbie at PHP but I am trying to learn, so thanks alot tongue.gif
Go to the top of the page
 
+Quote Post
good-baby
post May 2 2006, 11:48 AM
Post #7


Newbie [Level 3]
***

Group: Members
Posts: 48
Joined: 27-April 06
Member No.: 22,611



Buddy , a real nice sharing . Infact last night i was just wondering how to read files with .php extension.
Tonight i will just try your Tutorial and hope that shuld work for me. however , thanx for such a nice sharing ! biggrin.gif
Go to the top of the page
 
+Quote Post
Outsider
post May 2 2006, 04:37 PM
Post #8


Newbie
*

Group: Members
Posts: 8
Joined: 2-May 06
Member No.: 22,938



QUOTE(cmatcmextra @ Aug 26 2005, 10:46 AM) *

How To Read And Write And Files
a simple trick using php

CODE
<?
$thefile = "name.txt"; /* Our filename as defined earlier */
$towrite = "Black widgets rule!"; /* What we'll write to the file */

$openedfile = fopen($thefile, "w");
fwrite($openedfile, $towrite);
fclose($openedfile);
?>


Any questions about this tutorial?

What if I dont want to add the contents but I want to add something?

Can I move the file pointer to the end of the following and protect the previously entered contents from being deleted?

For example:

Say I have a website.
I have made a form in which people can send me messages.
I could use mail() to get it as a mail message.

But I want to store such info in a single file one after the other.
Is it possible?

Another question : Does it support multi-line passages?
i.e. Can many lines be sotred in a single file?
Go to the top of the page
 
+Quote Post
jlhaslip
post May 2 2006, 06:39 PM
Post #9


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,993
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



QUOTE
What if I dont want to add the contents but I want to add something?

Can I move the file pointer to the end of the following and protect the previously entered contents from being deleted?

Use $openedfile = fopen($thefile, "a+");
QUOTE

I have made a form in which people can send me messages.
I could use mail() to get it as a mail message.

NO, send the email is seperate function, but you could write the information to a file at the same time, then you have a copy in case the email fails to send or gets lost.
QUOTE

But I want to store such info in a single file one after the other.

See above. Check the link posted above that goes to the php.net page. All the possible choices are listed there. Use one that will 'append' to the file. either "a" or "a+" will work.
QUOTE

Another question : Does it support multi-line passages?

Yes, add newline and or return-line characters to the message body or use comma seperated values format with a unique character string as a seperator.
QUOTE

i.e. Can many lines be sotred in a single file?

A