Go Back   Gaming Gutter > Non-Gaming > Programming > Tutorials


Tutorials - Looking for programming tutorials to increase your knowledge? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post Please help me finding a...
Yesterday 11:13 PM
by _fam0us
Last post by seleporx
Today 07:19 AM
1 Replies, 14 Views
Go to first new post Price check - Program
Today 12:02 AM
Last post by xzNoobxz
Today 07:16 AM
7 Replies, 17 Views
Go to first new post Requesting score sender...
Yesterday 04:57 PM
by Ross
Last post by seleporx
Today 07:15 AM
3 Replies, 34 Views
Go to first new post A Petition!
10-10-2008 12:27 PM
by Axed
Last post by Hodizzle
Today 07:15 AM
49 Replies, 377 Views
Go to first new post Li-Shun Quits GG
10-08-2008 09:08 AM
by Li-Shun
Last post by Axed
Today 07:10 AM
188 Replies, 2,147 Views
Reply
 
LinkBack Thread Tools Display Modes

 Opening And Closing Files In PHP
Old 10-18-2006, 03:50 PM   #1 (permalink)
Oosband
Guest

 
Posts: n/a
iTrader: / %
Opening And Closing Files In PHP

To open a file we need this code:

Code:
<?php
if (!($f=fopen("file.txt","r+")))
exit("File Opening Error"); 
?>
Explination:
fopen() - this opens the file. The first section (file.txt) is the file name, the second (r+) defines how the file shall be opened, we will come back to this.
exit() - This displays an error message if the file fails to open.

Now, you saw the r+ mode? I'll explain all of these and what they are.

r - Read Only. File pointer at the start of the file.
r+ - Read and Write. File pointer at the start of the file.
w - Write Only. Overwrites the file...if the file doesn't exist then fopen() will attempt to create it.
w+ - Read and Write. Overwrites the file...if the file doesn't exist then fopen() will attempt to create it.
a - Append. File pointer at the end of the file. if the file doesn't exist then fopen() will attempt to create it.
a+ - Read and Append. File pointer at the end of the file. if the file doesn't exist then fopen() will attempt to create it.
x - Create and Open for Write Only. File pointer at the beginning of the file. If the file already exists, the fopen() call will fail and generate an error. If the file does not exist, it will attempt to create it.
x+ - Create and open for Read and Write. File pointer at the beginning of the file. If the file already exists, the fopen() call will fail and generate an error. If the file does not exist, it will attempt to create it.

If fopen() is unable to open the file, it will return as false (0).

---

Now to close the file...

fclose($f); - This closes the current open file.
  Reply With Quote

 Re: Opening And Closing Files In PHP
Old 11-19-2006, 10:30 PM   #2 (permalink)
Donor

Male krawk_are_cool is online now
 
krawk_are_cool's Avatar
 
Join Date: Sep 2006
Location: CA
Age: 23
Posts: 441
iTrader: 4 / 100%
krawk_are_cool Is gaining popularity
Rep Power: 7
Re: Opening And Closing Files In PHP

Nice tutorial. Thanks!
  Reply With Quote

 Re: Opening And Closing Files In PHP
Old 11-27-2006, 07:48 AM   #3 (permalink)
Junior Member

Male Xenos is offline
 
Join Date: Nov 2006
Posts: 19
iTrader: 0 / 0%
Xenos Is gaining popularity
Rep Power: 0
Re: Opening And Closing Files In PHP

Just a comment, you left out about the "b" flag for binary access.
  Reply With Quote

 Re: Opening And Closing Files In PHP
Old 12-02-2006, 02:38 PM   #4 (permalink)
Underground

Male gommle is offline
 
gommle's Avatar
 
Join Date: Sep 2006
Location: The o great land of Nooooooreeeway
Age: 20
Posts: 658
iTrader: 0 / 0%
gommle Is Recognizable
Rep Power: 7
Re: Opening And Closing Files In PHP

The b flag is used like this:
rb, wb+, xb+, etc.

You need the binary flag on Windows-hosts, but doesn't make fopen() slower on linux, so include the binary flag in all your scripts for compability.
  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Avatar Grabber Source Codes + Project Files. Flame Source Code 10 01-30-2008 06:08 AM
problems with .erl* files Yoda MapleStory 3 11-25-2006 03:12 PM
Opening mod apps again? whos taking myths place? Flame Chat 6 10-18-2006 03:17 AM
Can someone tell me how to download the library files? o.o Tropicanile Chat 3 09-23-2006 08:03 PM

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 07:20 AM.


vBulletin skin developed by: eXtremepixels
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.14490294 seconds (100.00% PHP - 0% MySQL) with 20 queries