Opening a Remote File in PHP
By: David Sklar Printer Friendly Format
Pass the file's URL to fopen():
$fh = fopen('http://www.example.com/robots.txt','r') or die($php_errormsg);
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.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
PHP code to import from CSV file to MySQL
PHP code to write to a CSV file from MySQL query
PHP code to write to a CSV file for Microsoft Applications
Password must include both numeric and alphabetic characters - Magento
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Error: Length parameter must be greater than 0
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Archived Comments
1. hello sir,i want to know that how to access the co
View Tutorial By: manish at 2011-12-17 07:40:07
2. Brettkip
View Tutorial By: Brettkip at 2017-06-01 10:30:02
3. Brettkip
View Tutorial By: Brettkip at 2017-07-20 00:23:02