WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Data Files Mon Jan 17, 2011 10:02 pm

Unknown Data

Unknown Data
Registered Member
Registered Member
Data Files
Learn PHP - Part 5


In some cases can it be a good idea to save the data in a textfile. It isn't so hard as it sounds. We are normally using these stuff with forums, counters ie. Just read the following and you'll hopefully learn how to do it.

Open, add and write in a textfile
We starts with a example.
Code:
<?php
$text = "This text will be written in a textfile";
$data = fopen("data.txt","w+");
fwrite = ($data, $text);
fclose($data);
?>
And how it works.
• By using the command fopen will our textfile data.txt and w+ save the content in the varaible $data.
• Now when we need to open the file data.txt can we use the fwrite command. In the parentheses is the file writtin, with the text that should be written.
• At the end will the file getting closed with fclose.

Read data from a textfile
This time do we got following code.
Code:
<?php
$data = fopen("data.txt","r");
$text = fgets($data, 50);
fclose ($data);
?>

And like before will I explain it, how it works.
• When we are going to read a file, are we using the mode r. We could also have used w+, but it isn't necassary when we just are going to read the file.
• To read the file, are we writting fgets. And the number 50 counts how many signs we wan't to read.
• With fclose will the file close.

Some modes
Here is a list with following modes in use with fopen().


• "r" The file will be opened in a reading statement.
• "r+" The file will be opened in a reading and writting statement.
• "w" The file will be opened in a writting statement and it'll delete all content (creates a new one if it's necassary).
• "w+" The file will be opened in a reading and writting statement and it'll delete all content (creates a new one if it's necassary).
• "a" The file will be opened in a writting statement and it'll delete all content (creates a new one if it's necassary).
• "a+" The file will be opened in a reading and writting statement and it'll delete all content (creates a new one if it's necassary).
• "x" Creates and open a writting statement file (supported by PHP 4.3.2 and later is it only working on local files).
• "x+" Creates and open a reading and writting statement file (supported by PHP 4.3.2 and later is it only working on local files).

In the next lesson will we take a look at a little counter, that can be used on your own website.

Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

http://woops.dk

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum