Home > php教程 > PHP开发 > body text

Example of accessing remote files through fopen() function in PHP

高洛峰
Release: 2016-12-21 15:50:57
Original
1846 people have browsed it

Using PHP not only allows users to access server-side files through the browser, but also access files in other servers through protocols such as HTTP or FTP. HTTP and FTP URLs can be used instead in most functions that require file names as parameters. file name. Use the fopen() function to bind the specified file name and resource to a stream. If the file name is in the format of "scheme://...", it is treated as a URL, and PHP will search for the protocol processor (also called the encapsulator protocol) to handle this mode.

If you need to access files remotely, you must activate the "allow_url_fopen" option in the PHP configuration file to use the fopen() function to open the remote file. It is also necessary to determine whether the files in other servers have access permissions. If the HTTP protocol is used to connect to the remote file, it can only be opened in "read-only" mode. If the remote FTP server that needs to be accessed has "writable permission" enabled for the user provided, then when using the FTP protocol to connect to the remote file, you can use the "write-only" or "read-only" mode to open the file. But you cannot use "Readable and writable" mode.

Using PHP to access remote files is the same as accessing local files. For example, you can use the following example to open a file on the remote web server and parse what we need. The output data can then be used in database retrieval, or simply output into style matching for the rest of the website. The code looks like this:

<?php
//通过http打开远程文件
$file = fopen(//m.sbmmt.com, "r") or die("打开远程文件失败!!");
while (!feof($file)){
    $line = fgets($file,1024);     //每读取一行
//如果找到远程文件中的标题标记则取出标题,并退出循环,不在读取文件
    if (preg_match("/<titile>(.*/)<\/title>",$line,$out)){     //使用正则匹配标题标记
        $title = $out[1];     //将标题标记中的标题字符取出
        break;     //退出循环,结束远程文件读取
    }
}
 
fclose($file);
echo $title;
?>
Copy after login

If you have legal access, you can use a The user's identity establishes a connection with an FTP server, so that the file on the FTP server can be written. This technology can be used to store remote log files, but this method can only be used to create new files. Overwriting an existing file, the call to the fopen() function will fail, and you need to connect to the server with a username other than anonymous, and you need to specify the username (or even password), such as "ftp://user:password@. ftp.lampbrother.net/path/to/file". The code is as follows:

<?php
    //在ftp.lampbrother.net的远程服务器上创建文件,以写的模式打开
    file = fopen("ftp://user:password@ftp.lapbrother.net/path/to/file", "w");
    //将一个字符串写入到远程文件中去
    fwrite($file, "Linux+Apache+MySQL+PHP");
 
    fclose($file);
?>
Copy after login

In order 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.

More For related articles on accessing remote files through the fopen() function in PHP, please pay attention to the PHP Chinese website

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!