php file_exists()函数用于检查服务器上是否存在一个文件或目录。但是,如果要检查远程服务器上的文件是否存在,file_exists()函数将不可用。
本文将介绍php中如何判断远程文件是否存在的几种方法。文章将演示并描述检查远程文件是否存在的各种方法。
- php curl() 方法
- php fopen() 方法
- php get_headers() 方法
- php file_get_contents() 方法
使用 php curl() 判断远程文件是否存在
curl是一个非常强大的开源库,支持很多协议,包括http、ftp、telnet等,我们使用它来发送http请求。它给我 们带来的好处是可以通过灵活的选项设置不同的http协议参数,并且支持https。curl可以根据url前缀是“http” 还是“https”自动选择是否加密发送内容。
php建立curl请求的基本步骤
- 初始化:curl_init()
- 设置属性:curl_setopt().有一长串curl参数可供设置,它们能指定url请求的各个细节。
- 执行并获取结果:curl_exec()
- 释放句柄:curl_close()
您可以使用curl来检查远程服务器上文件是否存在。下面的代码片段展示了如何在php中使用curl检查远程文件是否存在。
<?php function checkremotefile($url) { $ch = curl_init(); curl_setopt($ch, curlopt_url,$url); curl_setopt($ch, curlopt_nobody, 1); curl_setopt($ch, curlopt_failonerror, 1); curl_setopt($ch, curlopt_returntransfer, 1); if(curl_exec($ch)!==false) { return true; } else { return false; } } $url = 'http://www.manongzj.com/favicon.ico'; $check = checkremotefile($url); if(true == $check){ echo "ok"; }else{ echo "not ok"; } ?>
使用 php fopen() 判断远程文件是否存在
fopen() 函数用于打开一个文件或 url 地址。如果打开失败,本函数返回 false 。
语法:
resource fopen( string filename, string mode )
参数 filename 为尝试打开/创建的文件名,参数 mode 指定了打开模式,其可能的值如下:
打开模式 | 说明 |
---|---|
r | 只读,并将文件指针指向文件开始位置 |
r+ | 读写,将文件指针指向文件开始位置 |
w | 只写,将文件指针指向文件开始位置并将文件内容清空,如果文件不存在则尝试创建之 |
w+ | 读写,将文件指针指向文件开始位置并将文件内容清空,如果文件不存在则尝试创建之 |
a | 追加,将文件指针指向文件末尾来操作,如果文件不存在则尝试创建之 |
a+ | 读写追加,将文件指针指向文件末尾来操作,如果文件不存在则尝试创建之 |
x | 只写,并创建文件,如果文件已存在,则 fopen() 调用失败并返回 false |
x+ | 读写,并创建文件,如果文件已存在,则 fopen() 调用失败并返回 false |
下面的代码片段展示了如何使用php中的fopen()函数检查远程文件是否存在。
<?php // remote file url $remotefile = 'http://www.manongzj.com/favicon.ico'; // open file $handle = @fopen($remotefile, 'r'); // check if file exists if(!$handle){ echo 'file not found'; }else{ echo 'file exists'; } ?>
使用php get_headers()判断远程文件是否存在
get_headers() 是php系统级函数,他返回一个包含有服务器响应一个 http 请求所发送的标头的数组。如果失败则返回 false 并发出一条 e_warning 级别的错误信息(可用来判断远程文件是否存在)。
函数定义
array get_headers ( string $url [, int $format = 0 ] )
参数
format 如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名,如果设置默认为0
下面的代码片段展示了如何使用php中的get_headers()函数检查远程文件是否存在。
<?php function remote_file_exists($url){ return(bool)preg_match('~http/1\.\d\s+200\s+ok~', @current(get_headers($url))); } $ff = "http://www.manongzj.com/favicon.ico"; if(remote_file_exists($ff)){ echo "file exist!"; } else{ echo "file not exist!!!"; } ?>
使用php file_get_contents()判断远程文件是否存在
file_get_contents() 函数把整个文件读入一个字符串中。
和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
语法
file_get_contents(path,include_path,context,start,max_length)
参数
参数 | 描述 |
---|---|
path | 必需。规定要读取的文件。 |
include_path | 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。 |
context |
可选。规定文件句柄的环境。 context 是一套可以修改流的行为的选项。若使用 null,则忽略。 |
start | 可选。规定在文件中开始读取的位置。该参数是 php 5.1 新加的。 |
max_length | 可选。规定读取的字节数。该参数是 php 5.1 新加的。 |
下面的代码片段展示了如何使用php中的file_get_contents()函数检查远程文件是否存在。
<?php $url = 'http://www.manongzj.com'; $code = false; $options['http'] = array( 'method' => "head", 'ignore_errors' => 1 ); $body = file_get_contents($url, null, stream_context_create($options)); foreach($http_response_header as $header) sscanf($header, 'http/%*d.%*d %d', $code); echo "status code: $code"; ?>
如果你不想遵循重定向,你可以这样做:
<?php $url = 'http://www.manongzj.com'; $code = false; $options['http'] = array( 'method' => "head", 'ignore_errors' => 1, 'max_redirects' => 0 ); $body = file_get_contents($url, null, stream_context_create($options)); sscanf($http_response_header[0], 'http/%*d.%*d %d', $code); echo "status code: $code"; ?>
以上就是本文的全部内容,希望对大家的学习有所帮助。更多教程请访问码农之家
发表评论