Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> How Do I Create And Write To Files?, creating, writing, deleting files
alfonzo
post May 12 2005, 05:56 AM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 2
Joined: 12-May 05
From: Christchurch, New Zealand
Member No.: 5,002



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
Go to the top of the page
 
+Quote Post
sirknight
post May 12 2005, 06:16 AM
Post #2


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 20
Joined: 6-May 05
Member No.: 4,780



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:
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.
Go to the top of the page
 
+Quote Post
alfonzo
post May 14 2005, 03:30 AM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 2
Joined: 12-May 05
From: Christchurch, New Zealand
Member No.: 5,002



Thats great thanks smile.gif
Go to the top of the page
 
+Quote Post
JuseH
post May 15 2005, 02:03 AM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Banned
Posts: 6
Joined: 15-May 05
Member No.: 5,083



Look this: http://www.astahost.com/php-reading-and-wr...file-t4917.html
Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 25 2008, 10:12 PM
Post #5


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 22
Joined: 25-March 08
Member No.: 29,382




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 );
?>

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Creating A Game In Rpg Maker 2000/2003(17)
  2. Creating You Own Game In RPG Maker 2000/2003(12)
  3. Create Your Own Shout Box(12)
  4. How To: Connect, Read, Write, Close A Database(3)
  5. Help Me: Need To Transfer Files From Old Computer(18)
  6. Switch Network Settings With Batch Files(16)
  7. Forum Based Multiplayer RPG(10)
  8. How To Write A Virus ?(31)
  9. Creating Irc Chat Room...(9)
  10. Create A Site Without Cms But Just Dreamweaver?(6)
  11. Help Me Create A Text-based, Turn-based Game(8)
  12. Creating New Process Under Alternate Credentials (createprocessasuser)(5)
  13. The Cloning Issue(40)
  14. Can You Create A Folder Name "con"(14)
  15. New Browser Based Game, Create Or Conquer(4)
  1. Deleting A Corrupt File(24)
  2. Help Creating A Browser-based Rpg(5)
  3. How Do I Create Static Routes In Windows Xp?(10)
  4. Create Dynamic Gui ?(7)
  5. Creating Interactive Pdf Documents(2)
  6. Uploading More Than 30 Files In Less Than 10 Clicks?(5)
  7. Sandisk Memory Card Write Protection(12)
  8. Easiest Free Forum To Create Custom Skin For?(2)
  9. Creating Links With Quotes (")(2)
  10. 3d Game Creating Thing(1)
  11. Need Some Help/advice On Lost Files.(8)
  12. Need Some Help/advice On How To Restore Lost Files(3)
  13. How Do You Create A Vista?(6)


 



- Lo-Fi Version Time is now: 7th July 2008 - 03:45 PM