As long as the allow_url_fopen option is activated in the php.ini file, you can use HTTP and FTP URLs in place of the file names in most functions that require file names as parameters. You can also use URLs in include, include_once, require, and require_once statements. For more information on the protocols supported by PHP, see supported protocols and encapsulation protocols.
Attention:
To use the URL encapsulation protocol in PHP 4.0.3 and its earlier versions, you need to configure PHP with the--enable-url-fopen-wrapper parameter at compile time.
Remote access to the following functions is not supported by the Windows version of PHP 4.3 before: Include, include_once, require, require_once, and imagecreatefromxxx in the GD and Image functions Number.
For example, you can use the following example to open a file on a remote Web server, parse the required output data, and then use that data in a database retrieval, or simply export its content in the same style as the other pages of your site.
Example #1 Get the title of a remote file
<?php $file = fopen ("/", "R"); if (! $file) { echo "<p>unable to open remote file.\n"; Exit; } while (!feof ($file)) { $line = fgets ($file, 1024x768); /* This is only works if the title and its tags is on a line */ if (eregi ("<title> (. *) </title>", $line, $ ) { $title = $out [1]; break; } } Fclose ($file);? >
If you have legitimate access rights, you establish a link with an FTP server as a user, and you can write to the FTP server-side files. Only this method can be used to create a new file, and if an attempt is made to overwrite a file that already exists, the call to the fopen () function will fail.
To connect to a server with a user name other than "anonymous", you need to indicate the user name (and possibly a password), such as "Ftp://user:password@ftp.example.com/path/to/file" (which can also be The HTTP protocol uses the same syntax when accessing remote files).
Example #2 saving data to a remote server
<?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);? >
Attention:
You might be inspired by the example above to store remote log files with this technique. However, as mentioned above, only the new file can be written in a URL opened with the fopen () method. If the remote file already exists, the operation of the fopen () function will fail. To do something similar to distributed logging, you can refer to the Syslog () function.