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. You can
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. You can
Using PHP not only allows users to access files on the server through a browser, but also allows users to access files on other servers through HTTP or FTP protocols, 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 (, "r") or die ("An error occurred while opening the 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.