PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




fopen

Name

fopen — Opens a file.

Synopsis

resource fopen(filename, mode);
string filename: Filename or URL to open
string mode: File mode

Returns

File handle

Description

fopen() opens a file on the current system and positions the file pointer at the beginning of the file. If PHP has been configured to be URL-aware, prefixing the filename with either http:// or ftp:// opens a connection to the specified URL and returns the contents of the specified file. HTTP requests are not redirected, so any requests to a directory must have a trailing slash appended and FTP connections must be made to servers with support for passive mode transfers. The mode strings can be one of the following:

Mode String Function
r Read only
r+ Read and write, preserves file contents (overwrites existing content on write operations)
w Open and truncate existing file to zero length or create a new file
w+ Open for read and write, truncate existing file to zero length or create a new file
a Read only, create new file or append to end of existing file
a+ Read and write, create new file or append to end of existing file
The umask of the created file can be modified with the umask() function.

Note

You cannot write to a URL.

Warning

3.0.7: Doesn't always work with URLs on Red Hat Linux 5.2. Reported to crash sometimes or make PHP return empty documents when using local files on Red Hat Linux 5.9.

3.0.8: Sometimes causes PHP to crash when using URLs on Linux 2.0.35. Subsequent fopen() or fclose() calls won't work on URLs on Solaris 2.6.

3.0.9: Reported not to work with HTTP files on Linux. Doesn't reliably work when supplying username/password information in URLs on Linux

3.0.10: Simply trying to open the file "http://" crashes PHP on Linux and Windows.

3.0.11: Might hang on Windows after reading more than 511 characters from a URL. Reported problems with URL wrappers again.

3.0.12: Might segfault with a certain compilation combination of Apache/PHP on Red Hat Linux 5.2.

Example

Example 322. Dump index page of a WWW server

$fh = fopen("http://www.host.com/", "r");

while(!feof($fh))
{
    $output = htmlspecialchars(fgets($fh, 1024));
    echo ("$output<br />");
}

fclose($fh);



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.