Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Handling files with PHP4 - Part1

July 22, 2002

Basic file handling is a fundamental part of programming. Even now, with powerful and easy to use Database Management Systems available to manage your files, manipulating files in your own code is often necessary. Part 1 of this article explains how to read, write and process files with PHP4.

Opening and Closing Files

File handling usually involves three steps. The file is opened, then put to use (whether reading or writing), and finally closed. All files should be opened before they are used, and closed when you've finished with them. Some of the functions do this automatically, but it's important to get into the habit from the beginning.

To open a file, you need to know what you're going to be doing with the file (reading or writing), and of course the name of the file. The PHP function to do this is fopen, and is used as follows:

int fopen (string filename, string mode [, int use_include_path])

Mode can be any of the following:

rOpens the file for reading only, starting at the beginning of the file.
r+Opens the file for reading and for writing, starting at the beginning of the file.
'wOpens the file for writing only. This overwrites an existing file, or creates a new file.
w+Opens the file for reading and for writing. This overwrites an existing file, or creates a new file.
aOpens the file for writing only. This appends to (starts at the end of) an existing file, or creates a new one if none is present.
a+Opens the file for reading for writing. This appends to (starts at the end of) an existing file, or creates a new one if none is present.
bIndicates that the file is a binary file (such as images and sound files, as opposed to text files such as your PHP scripts). Some operating systems do not distinguish. Windows does, but Unix doesn't, in which case the flag makes no difference.

The fopen function returns an integer called the file handle, which is used in the other functions we'll look at later, or false if the open failed for any reason. This means we can use it in a safe way that deals with errors, as follows:

<?	
$filename = "/home/ian/hiddensecrets.txt";
if (!($fp = fopen ($filename, "w"))) {
print "Error - could not open the file $filename";
}
else {
	// process the file...
}
?>

Be careful with Windows pathnames. If you use a backslash (\), you'll need to escape it. You can also use forward slashes (/) for Windows pathnames.

<?
$filename = "c:\\images\\lion.gif";
if (!($fp = fopen ($filename, "rb"))) {
	print "Error - could not open the file $filename";
}
else {
	// process the file...
}
?>

PHP also usefully allows you to read files on remote servers. By beginning the filename with http:// the server connects to the remote server via HTTP 1.0 in order to open the file. Similarly, prepending ftp:// opens an ftp connection. The ftp server must support passive ftp, otherwise the connection will fail. Note that early versions of PHP4 (before version 4.0.5) did not support HTTP redirects, which means you'd need to put the trailing slash at the end of the filename.

The optional parameter at the end of the function indicates whether PHP should search the directories in the the include path (set in php.ini). It can be set to '1' or TRUE, otherwise it defaults to FALSE.

<?
$filename = "http://lionsandtigers.co.za/images/tiger.jpg";
if (!($fp = fopen ($filename, "rb"))) {
	print "Error - could not open the file $filename";
}
else {
	// process the file...
}
?>

Closing a file is easy - just call the fclose function with file handler passed to you when the file was opened. Closing files may seem pointless, especially as you're unlikely to notice a difference when testing with small scripts like these. Leaving files open unecessarily just puts extra load on your systems, and is bound to bite you if you're working on larger systems.

bool fclose (int fp)

<?	
$filename = "/home/ian/hiddensecrets.txt";
if (!($fp = fopen ($filename, "w"))) {
	print "Error - could not open the file $filename";
}
else {
	// process the file...
	fclose($fp);
}
?>

Although fclose() returns true or false, most code doesn't check for the possible failure of this operation unless it's absolutely critical to know. Besides, the vast majority of failures occur when opening a file, (invalid filenames or paths etc.), while the kinds of errors that can cause your successfully opened file not to close, (disk failures etc.), are not the kind of thing your code can do much about.

Download the scripts from this article

Displaying files - Page 2


Up to => Home / Authoring / Languages / PHP




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers