Tuesday, April 20, 2010

2:33 AM

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/”, “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/”, “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/’);


// 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);
?> 

0 comments: