php

php检查远程服务器文件是否存在

注:该方法需要curl的支持,使用前请在php.ini 里找到“extension=php_curl.dll” 将前面的注释去掉,重启服务,ok!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function check_remote_file_exists($url)
{
$curl = curl_init($url);
// 不取回数据
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); //不加这个会返回403,加了才返回正确的200,原因不明
// 发送请求
$result = curl_exec($curl);
$found = false;
// 如果请求没有发送失败
if ($result !== false)
{
// 再检查http响应码是否为200
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200)
{
$found = true;
}
}
curl_close($curl);

return $found;
}

$exists = check_remote_file_exists('http://community.chinahrd.net/uc_server/data/avatar/001/04/40/

40_avatar_small.jpg'
);
echo $exists ? '存在' : '不存在';
分享到:

3 条评论

点击这里取消回复。

昵称
  1. 吉微盘

    我来八度。呵呵 吉微盘:http://www.jiweipanw.com

  2. Mikeyzm

    我来挖坟啦~

  3. carl

    很有用的东西!