Using remote files in PHP

As long as allow_url_fopen is enabled in php.ini, you can use HTTP and FTP URLs with most of the functions that take a filename as a parameter. In addition, URLs can be used with the include(), include_once(), require() and require_once() statements. In PHP 4.0.3 and older, in order to use URL wrappers, you were required to configure PHP using the configure option –enable-url-fopen-wrapper.

Getting the title of a remote page
For example, you can use code below to to open a file from a remote web server and extract title of the page.

<?php
$file = fopen (“http://www.example.com/&#8221;, “r”);
if (!$file) {
    echo “<p>Unable to open remote file.\n”;
    exit;
}
while (!feof ($file)) {
    $line = fgets ($file, 1024);
    /* This only works if the title and its tags are on one line */
    if (eregi (“<title>(.*)</title>”, $line, $out)) {
        $title = $out[1];
        break;
    }
}
fclose($file);
?> 

Reading the contents of a remote file
fread function can be used to read content of remote file. This is shown in example below. You should collect the data together in chunks as shown in the example below because reading will stop after a packet is available.

<?php
$handle = fopen(“http://www.example.com/&#8221;, “rb”);
$contents = ”;
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
?> 

reading the contents of a file into array
file function returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Each line in the resulting array will include the line ending, so you still need to use rtrim() if you do not want the line ending present.

<?php
// Get a file into an array.  In this example we’ll go through HTTP to get
// the HTML source of a URL.
$lines = file(‘http://www.example.com/&#8217;);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    echo “Line #<b>{$line_num}</b> : ” . htmlspecialchars($line) . “<br />\n”;
}
?>

reading the contents of a file into a string

file_get_contents() returns the file in a string. This is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

<?php
$content_string = file_get_contents(‘www.example.com/products.html’);
?>

Storing data on a remote server
You can also write to files on an FTP server (provided that you have connected as a user with the correct access rights). You can only create new files using this method; if you try to overwrite a file that already exists, the fopen() call will fail. You need to specify the username and password within the URL, such as ‘ftp://user:password@ftp.example.com/path/to/file’.

<?php
$file = fopen (“ftp://ftp.example.com/incoming/outputfile”, “w”);
if (!$file) {
    echo “<p>Unable to open remote file for writing.\n”;
    exit;
}
/* Write the data here. */
fwrite ($file, $_SERVER[‘HTTP_USER_AGENT’] . “\n”);
fclose ($file);
?> 

About these ads

7 Responses to Using remote files in PHP

  1. sarmad says:

    i need all ftp file also fine that are using authentication , plz reply as soon as possible

    thank Your

    sarmad mahar

  2. Prashant says:

    oh thanks its really helpful for me to learn php fundamentals..

  3. Kamal says:

    thanks for the info mate..

  4. sbomag says:

    I know this page is a bit dated, but its exactly what I needed -getting remote titles. Thanks!

  5. Adriana says:

    hi

    when i do the uploading of a file, i can’t change permissions from 644.

    Can someone say what should be the problem?

    Thanks

  6. jay says:

    write remote php file in website

    http://www.listofgods.com/time.php

    time.php is change any time

  7. all the time i used to read smaller posts that as well clear their motive, and that is also happening with this paragraph which I am reading at this time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: