PHP中通过fopen()函数访问远程文件示例_php技巧
使用PHP不仅可以让用户通过浏览器访问服务器端的文件,还可以通过HTTP或FTP等协议访问其他服务器中的文件,可以在大多数需要用文件名作为参数的函数中使用HTTP和FTP URL来代替文件名。使用fopen()函数将指定的文件名与资源绑定到一个流上,如果文件名是“scheme://…”的格式,则被当成一个URL,PHP将搜索协议处理器(也被成为封装协议)来处理此模式。
如果需要远程访问文件,必须在PHP的配置文件中激活“allow_url_fopen”选项,才能使用fopen()函数打开远程文件。而且还要确定其他服务器中的文件是否有访问权限,如果使用HTTP协议对远程文件进行连接,只能以“只读”模式打开。如果需要访问的远程FTP服务器中,对所提供的用户开启了“可写权限,则使用FTP协议连接远程文件时,就可以使用“只写”或“只读”模式打开文件。但不可以使用“可读可写”模式。
使用PHP访问远程文件就像访问本地文件一样,都是使用相同的读写函数处理。例如,可以用以下范例来打开远程Web服务器上的文件,解析我们需要的输出数据,然后就将这些数据用在数据库的检索中,或者简单地将其输出到网站剩下内容的样式匹配中。代码如下所示:
//通过http打开远程文件
$file = fopen(http://www.jb51.net, "r") or die("打开远程文件失败!!");
while (!feof($file)){
$line = fgets($file,1024); //每读取一行
//如果找到远程文件中的标题标记则取出标题,并退出循环,不在读取文件
if (preg_match("/
$title = $out[1]; //将标题标记中的标题字符取出
break; //退出循环,结束远程文件读取
}
}
fclose($file);
echo $title;
?>
如果有合法的访问权限,可以以一个用户的身份和某FTP服务器建立连接,这样就可以向该FTP服务器端的文件进行写操作了。可以用该技术来存储远程日志文件等操作,但仅能用该方法来创建新的文件,如果尝试覆盖已经存在的文件,fopen()函数的调用将会失败。而且要以匿名(anonymous)以外的用户名连接服务器,并需要指明用户名(甚至密码),例如“ftp://user:password@ftp.lampbrother.net/path/to/file”。代码如下所示:
//在ftp.lampbrother.net的远程服务器上创建文件,以写的模式打开
file = fopen("ftp://user:password@ftp.lapbrother.net/path/to/file", "w");
//将一个字符串写入到远程文件中去
fwrite($file, "Linux+Apache+MySQL+PHP");
fclose($file);
?>
为了避免由于访问远程主机时发生的超时错误,可以使用set_time_limit()函数对程序的运行时间加以限制。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
