Home > Backend Development > PHP Tutorial > PHP uses remote HTTP file and FTP file operations

PHP uses remote HTTP file and FTP file operations

伊谢尔伦
Release: 2016-11-22 10:25:44
Original
1636 people have browsed it

As long as the allow_url_fopen option is activated in the php.ini file, you can use HTTP and FTP URLs instead of file names in most functions that require file names as parameters. At the same time, URLs can also be used in include, include_once, require and require_once statements. For more information on the protocols supported by PHP, see Supported Protocols and Encapsulated Protocols.

Note:

To use the URL wrapping protocol in PHP 4.0.3 and earlier versions, you need to configure PHP with the --enable-url-fopen-wrapper parameter at compile time.

The Windows version of PHP before version 4.3 does not support remote access to the following functions: include, include_once, require, require_once and the imagecreatefromXXX function in GD and Image functions.

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 this data in database retrieval, or simply output its content in the same style as other pages on your website.

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, 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);
?>
Copy after login

If you have legal access rights and establish a link with an FTP server as a user, you can also write to the file on the FTP server. This method can only be used to create new files; if you try to overwrite an existing file, the call to the fopen() function will fail.

To connect to the server with a username other than "anonymous", you need to specify the username (and possibly password), such as "ftp://user:password@ftp.example.com/path/to/file" (you can also Use the same syntax when accessing remote files via the HTTP protocol that requires Basic authentication).

Example #2 Save 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[&#39;HTTP_USER_AGENT&#39;] . "\n");
    fclose ($file);
?>
Copy after login

Note:

You may be inspired by the above example and use this technology to store remote log files. But as mentioned above, in a URL opened with fopen(), only new files can be written. 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.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template