Example of accessing remote files through the fopen of function in PHP

  • 2021-08-03 09:37:49
  • OfStack

Using PHP allows users not only to access server-side files through browsers, but also to access files in other servers through protocols such as HTTP or FTP. HTTP and FTP URL can be used instead of file names in most functions that need file names as parameters. Use the fopen () function to bind the specified file name and resource to a stream. If the file name is in the format "scheme://…", it is treated as an URL, and PHP searches the protocol handler (also known as the encapsulation protocol) to handle this pattern.

If you need remote access to the file, you must activate the "allow_url_fopen" option in the PHP configuration file to open the remote file using the fopen () function. But also determine whether the files in other servers have access rights. If you use HTTP protocol to connect to remote files, you can only open them in "read-only" mode. If you want to access a remote FTP server with writable permissions enabled for the provided user, you can use write-only or read-only mode to open the file when connecting to the remote file using the FTP protocol, but you cannot use read-write mode.

Access to remote files using PHP is done using the same read-write function as access to local file 1. For example, you can use the following example to open a file on a remote Web server, parse the output data we need, and then use this data for database retrieval, or simply output it to the style matching of the rest of the website. The code looks like this:


<?php
// Pass http Open a remote file
$file = fopen(https://www.ofstack.com, "r") or die(" Failed to open remote file! ! ");
while (!feof($file)){
    $line = fgets($file,1024);     // Per read 1 Row
// If the header tag in the remote file is found, take out the header and exit the loop without reading the file
    if (preg_match("/<titile>(.*/)<\/title>",$line,$out)){     // Use regular matching header tags
        $title = $out[1];     // Take out the title character in the title tag
        break;     // Exit the loop and end the remote file reading
    }
}
 
fclose($file);
echo $title;
?>

If you have legal access rights, you can establish a connection with an FTP server as a user, so that you can write files to the FTP server. You can use this technique to store operations such as remote log files, but you can only use this method to create new files. If you try to overwrite an existing file, the call to the fopen () function will fail. Also, to connect to the server with a user name other than anonymous (anonymous), you need to indicate the user name (even password), such as "ftp://user: password @ ftp. lampbrother. net/path/to/file". The code looks like this:


<?php
    // In ftp.lampbrother.net Create a file on the remote server of, and open it in write mode
    file = fopen("ftp://user:password@ftp.lapbrother.net/path/to/file", "w");
    // Will 1 Strings are written to a remote file
    fwrite($file, "Linux+Apache+MySQL+PHP");
 
    fclose($file);
?>

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


Related articles: