Jump to content
xisto Community
electriic ink

How To Read And Write Files Using Php php :: reading and writing files

Recommended Posts

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:

 

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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+");

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.

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.

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.

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.

Share this post


Link to post
Share on other sites

i have to send ip address to some service provider

How To Read And Write Files Using Php

 

Hi dude

 

I want to pass the ip address to any service provider, in that they have to send which country it belongs thats one I need ex:india,usa like that if anyone knows please help me..

Advance thanks who help me thank you

 

-vignesh

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.