This article mainly introduces the example of using the fopen () function to access remote files in PHP. This article describes the functions of the fopen function, the configuration problems required for using it, and timeout issues, the code example is provided. if you need it, you can refer to using PHP to not only allow users to access files on the server through a browser, but also access files on other servers through HTTP or FTP, you can use HTTP and FTP URLs to replace file names in most functions that require file names as parameters. Use the fopen () function to bind the specified file name to the resource to a stream. if the file name is "scheme ://..." Is treated as a URL. PHP will use the search protocol processor (also known as Encapsulation Protocol) to process this mode.
To remotely access a file, you must activate the "allow_url_fopen" option in the PHP configuration file to use the fopen () function to open the remote file. In addition, you must determine whether the files on other servers have access permissions. if you use HTTP to connect to remote files, you can only open the files in "read-only" mode. If the "write permission" is enabled for the provided users on the remote FTP server to be accessed, you can use the "write-only" or "read-only" mode to open the file. However, the "readable and writable" mode cannot be used.
Using PHP to access remote files is the same as accessing local files. For example, you can use the following example to open files on a remote Web server, parse the output data we need, and then use the data for database retrieval, or simply output it to the style matching of the remaining content of the website. The code is as follows:
The code is as follows:
<? Php
// Open a remote file over http
$ File = fopen (http://www.bitsCN.com, "r") or die ("failed to open remote file !! ");
While (! Feof ($ file )){
$ Line = fgets ($ file, 1024); // read each row
// If the title mark in the remote file is found, the title is taken out and the loop is exited.
If (preg_match ("/ (. */) <\/Title> ", $ line, $ out) {// use regular expression matching to mark the title
$ Title = $ out [1]; // extract the title characters in the title tag
Break; // exit the loop to end remote file reading
}
}
Fclose ($ file );
Echo $ title;
?>
If you have valid access permissions, you can establish a connection with an FTP server as a user, so that you can perform write operations on the file on the FTP server. You can use this technology to store remote log files and other operations, but you can only use this method to create new files. if you try to overwrite existing files, the fopen () function will fail to be called. And connect to the server with a username other than anonymous (anonymous) and specify the username (or even password), such as "ftp: // user: password@ftp.lampbrother.net/path/to/file ". The code is as follows:
The code is as follows:
<? Php
// Create a file on the ftp.lampbrother.net remote server and open it in write mode
File = fopen ("ftp: // user: password@ftp.lapbrother.net/path/to/file", "w ");
// Write a string to a remote file
Fwrite ($ file, "Linux + Apache + MySQL + PHP ");
Fclose ($ file );
?>
To avoid timeout errors when accessing the remote host, you can use the set_time_limit () function to limit the running time of the program.