Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

How To Read And Write Files Using Php


23 replies to this topic

#1 electriic ink

    "Britons never never shall be slaves." As true now as it was in 1740.

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,262 posts
  • Gender:Male
  • Location:Heaven
  • Interests:Promotion: Aug 4 2005 8.24pm BST
  • myCENT:74.43

Posted 26 August 2005 - 10:46 AM

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
  • Create a text file called name.txt in any directory.
  • Change the file permissions to 777
  • 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 ;)


Open The File

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

<? 

$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:

php.net said:


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:

<? 

$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:

<?
$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:

<?
$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 :P

Any questions about this tutorial?

#2 wariorpk

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 661 posts

Posted 26 August 2005 - 11:33 AM

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.

#3 neokid

    Newbie

  • Kontributors
  • Pip
  • 9 posts

Posted 27 August 2005 - 01:48 AM

OMG thank you, i had alot of trouble but im a beginner

#4 shadowdemon

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 209 posts

Posted 17 September 2005 - 11:31 AM

were do u write in it

#5 thablkpanda

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 339 posts
  • Location:Atlanta, GA

Posted 17 September 2005 - 04:19 PM

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

#6 Amezis

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Location:Oslo, Norway

Posted 17 September 2005 - 04:27 PM

Great tutorial :P

I'm only a newbie at PHP but I am trying to learn, so thanks alot :P

#7 good-baby

    Newbie [Level 3]

  • Kontributors
  • PipPipPip
  • 48 posts

Posted 02 May 2006 - 11:48 AM

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 ! <_<

#8 Outsider

    Newbie

  • Kontributors
  • Pip
  • 8 posts

Posted 02 May 2006 - 04:37 PM

View Postcmatcmextra, on Aug 26 2005, 10:46 AM, said:

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

<?
$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?

#9 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 02 May 2006 - 06:39 PM

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?

As many as the File Manager will allow you to have for your system on the server. On a LInux based Apache server like the one here at the Trap, LOTS.

#10 Outsider

    Newbie

  • Kontributors
  • Pip
  • 8 posts

Posted 03 May 2006 - 04:12 PM

Thanks for all the support. I will be putting the above tips into action.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users