PHP fopen () function to access the remote file sample _php Tips

Source: Internet
Author: User
Tags anonymous remote ftp server ftp protocol

Using PHP not only allows users to access server-side files through the browser, but also access files from other servers via protocols such as HTTP or FTP, and can use HTTP and FTP URLs instead of filenames in most functions that require a filename as a parameter. Use the fopen () function to bind the specified file name and resource to a stream, and if the file name is a format of "scheme://...", it is treated as a url,php the Search protocol processor (also become the encapsulation protocol) to handle this pattern.

If you need to access files remotely, you must activate the "allow_url_fopen" option in the PHP configuration file to open the remote file using the fopen () function. Also, determine if the files in the other server have access rights, and if you connect to the remote file using the HTTP protocol, you can only open it in read-only mode. If you have access to a remote FTP server that has the Write permission enabled for the provided user, you can use write-only or read-only mode to open the file when you connect to the remote file using the FTP protocol. However, you cannot use the readable writable mode.

Using PHP to access a remote file is like accessing a local file, using the same read-write function. For example, you can use the following example to open a file on a remote Web server, parse the output data that we need, and then use that data in the retrieval of the database, or simply output it to a style match for the rest of the site. The code looks like this:

Copy Code code as follows:

<?php
Open remote files via HTTP
$file = fopen (http://www.jb51.net, "R") or Die ("Open remote file failed!! ");
while (!feof ($file)) {
$line = fgets ($file, 1024); Each read row
If the header tag in the remote file is found, the caption is removed and the loop is exited, not reading the file
if (Preg_match (. */) <\/title>, $line, $out)) {//using regular matching header tags
$title = $out [1]; Remove the caption character from the title tag
Break Exit loop, end remote file read
}
}

Fclose ($file);
echo $title;
?>

If you have legitimate access rights, you can establish a connection to an FTP server as a user, so that you can write to the file on that FTP server side. You can use this technique to store operations such as remote log files, but you can only use this method to create new files, and calls to the fopen () function will fail if you attempt to overwrite files that already exist. Also, connect to the server with a user name other than anonymous (anonymous), and need to specify a username (or even a password), such as "Ftp://user:password@ftp.lampbrother.net/path/to/file". The code looks like this:

Copy Code code as follows:

<?php
Create a file on a ftp.lampbrother.net remote server, open in write mode
File = fopen ("Ftp://user:password@ftp.lapbrother.net/path/to/file", "w");
Writes a string to a remote file
Fwrite ($file, "linux+apache+mysql+php");

Fclose ($file);
?>

To avoid timeout errors that occur when accessing a remote host, you can use the Set_time_limit () function to limit the running time of the program.

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.