Home php教程 PHP开发 Example of accessing remote files through fopen() function in PHP

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

Dec 21, 2016 pm 03:50 PM
fopen

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(http://www.php.cn, "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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X Aug 25, 2023 am 09:22 AM

How to solve PHPWarning:fopen():SSLoperationfailedinfile.phponlineX In PHP programming, we often use the fopen function to open files or URLs and perform related operations. However, when using the fopen function, sometimes you will encounter something similar to Warning:fopen():SSLoperationfailedinfile.p

How to solve PHP Warning: fopen(): failed to open stream: Permission denied How to solve PHP Warning: fopen(): failed to open stream: Permission denied Aug 20, 2023 pm 01:45 PM

How to solve PHPWarning:fopen():failedtoopenstream:Permissiondenied In the process of developing PHP programs, we often encounter some error messages, such as PHPWarning:fopen():failedtoopenstream:Permissiondenied. This error is usually due to incorrect file or directory permissions

How to solve PHP Warning: fopen(): failed to open stream: No such file or directory How to solve PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory In the process of using PHP development, we often encounter some file operation problems, one of which is "PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory"

Usage of fopen function in Matlab Usage of fopen function in Matlab Nov 28, 2023 am 11:03 AM

In Matlab, the fopen function is used to open a file and return the file identifier for subsequent reading or writing operations on the file. Select the appropriate permission options to open the file as needed, and promptly close the file when the operation is complete. It should be noted that after opening a file, you need to ensure that the file is closed in time when it is no longer needed to release system resources. In addition, if the file opening fails or an operation error occurs, the error handling mechanism can be used to handle it accordingly.

In C language, use the fopen() function to open an existing file in write mode In C language, use the fopen() function to open an existing file in write mode Aug 27, 2023 pm 10:33 PM

The fopen() method in C is used to open the specified file. Let's take an example to understand the problem syntax FILE*fopen(filename,mode). The following are valid modes for using fopen() to open files: 'r', 'w', 'a', 'r+', 'w+', ' a+'. For more information, please visit the C library function-fopen()

如何解决PHP Warning: fopen(): failed to open stream: No such file or directory in file.php on line X 如何解决PHP Warning: fopen(): failed to open stream: No such file or directory in file.php on line X Aug 26, 2023 pm 12:46 PM

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectoryinfile.phponlineX When developing and running PHP programs, we sometimes encounter PHPWarning:fopen():failedtoopenstream:Nosuchfileor

How to use fopen, fwrite and fclose in php for file operations? How to use fopen, fwrite and fclose in php for file operations? Jun 01, 2023 am 08:46 AM

In PHP development, file operations are very common. Under normal circumstances, we need to perform file reading, writing, deletion and other operations. Among them, the fopen function and fread function can be used to read the file, and the fopen function, fwrite function and fclose function can be used to write the file. This article will introduce how PHP uses fopen, fwrite and fclose to perform file operations. 1. fopen function The fopen function is used to open files. Its syntax is as follows: r

What is the usage of fopen function in php What is the usage of fopen function in php Sep 18, 2021 pm 03:43 PM

The usage of fopen function in php is "fopen(filename, mode, include_path, context)". The fopen() function is used to open a file or URL. If it fails, it returns false with an error message.

See all articles