PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 18.4 Opening a Remote File

18.4.1 Problem

You want to open a file that's accessible to you via HTTP or FTP.

18.4.2 Solution

Pass the file's URL to fopen( ):

$fh = fopen('http://www.example.com/robots.txt','r') or die($php_errormsg);

18.4.3 Discussion

When fopen( ) is passed a filename that begins with http://, it retrieves the given page with an HTTP/1.0 GET request (although a Host: header is also passed along to deal with virtual hosts). Only the body of the reply can be accessed using the file handle, not the headers. Files can be read, not written, via HTTP.

When fopen( ) is passed a filename that begins with ftp://, it returns a pointer to the specified file, obtained via passive mode FTP. You can open files via FTP for either reading or writing, but not both.

To open URLs that require a username and a password with fopen( ), embed the authentication information in the URL like this:

$fh = fopen('ftp://username:password@ftp.example.com/pub/Index','r');
$fh = fopen('http://username:password@www.example.com/robots.txt','r');

Opening remote files with fopen( ) is implemented via a PHP feature called the URL fopen wrapper. It's enabled by default but is disabled by setting allow_url_fopen to off in your php.ini or web server configuration file. If you can't open remote files with fopen( ), check your server configuration.

18.4.4 See Also

Section 11.2 through Section 11.6, which discuss retrieving URLs; documentation on fopen( ) at http://www.php.net/fopen and on the URL fopen wrapper feature at http://www.php.net/features.remote-files.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    18.1 Introduction
    Recipe 18.2 Creating or Opening a Local File
    Recipe 18.3 Creating a Temporary File
    Recipe 18.4 Opening a Remote File
    Recipe 18.5 Reading from Standard Input
    Recipe 18.6 Reading a File into a String
    Recipe 18.7 Counting Lines, Paragraphs, or Records in a File
    Recipe 18.8 Processing Every Word in a File
    Recipe 18.9 Reading a Particular Line in a File
    Recipe 18.10 Processing a File Backward by Line or Paragraph
    Recipe 18.11 Picking a Random Line from a File
    Recipe 18.12 Randomizing All Lines in a File
    Recipe 18.13 Processing Variable Length Text Fields
    Recipe 18.14 Reading Configuration Files
    Recipe 18.15 Reading from or Writing to a Specific Location in a File
    Recipe 18.16 Removing the Last Line of a File
    Recipe 18.17 Modifying a File in Place Without a Temporary File
    Recipe 18.18 Flushing Output to a File
    Recipe 18.19 Writing to Standard Output
    Recipe 18.20 Writing to Many Filehandles Simultaneously
    Recipe 18.21 Escaping Shell Metacharacters
    Recipe 18.22 Passing Input to a Program
    Recipe 18.23 Reading Standard Output from a Program
    Recipe 18.24 Reading Standard Error from a Program
    Recipe 18.25 Locking a File
    Recipe 18.26 Reading and Writing Compressed Files
    Recipe 18.27 Program: Unzip
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele