Php operations using remote HTTP files and FTP files

Source: Internet
Author: User
Tags ftp file
PHP features using remote HTTP file and FTP file operations only in php. if the allow_url_fopen option is activated in the ini file, HTTP and FTP URLs can be used in most functions that require file names as parameters to replace file names. You can also use the URL in the include, include_once, require, and require_once statements. For more information about the Protocols supported by PHP, see Supported protocols and encapsulation protocols.

Note:

To use the URL Encapsulation Protocol in PHP 4.0.3 and earlier versions, you must use the -- enable-url-fopen-wrapper parameter to configure PHP during compilation.

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

For example, you can use the following example to open files on a remote web server, parse the required output data, and then use the data for database retrieval, or simply output the content in the same style as other pages on your website.

Example #1 obtain the remote file title

 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 ("(.*)", $line, $out)) {            $title = $out[1];            break;        }    }    fclose($file);?>

If you have valid access permissions, you can establish a connection with an FTP server as a user and write data to the file on the FTP server. You can only use this method to create a new file. if you try to overwrite an existing file, call the fopen () function will fail.

To connect to the server with a user name other than "anonymous", you need to specify the user name (which may have a password), such as "ftp: // user: password@ftp.example.com/path/to/file "(you can also use the same syntax when accessing remote files through an HTTP protocol that requires Basic authentication ).

Example #2 save the data to a remote server

 Unable to open remote file for writing.\n";        exit;    }    /* Write the data here. */    fwrite ($file, $_SERVER['HTTP_USER_AGENT'] . "\n");    fclose ($file);?>

Note:

It may be inspired by the above example to use this technology to store remote log files. However, as mentioned above, in the URL opened in fopen () mode, only the new file can be written. If the remote file already exists, the operation of the fopen () function will fail. To do something similar to distributed logs, refer to the syslog () function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.