代码如下:
//判断远程文件
function check_remote_file_exists($url)
{
$curl = curl_init($url);
// 不取回数据
curl_setopt($curl, curlopt_nobody, true);
// 发送请求
$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;
}
最近在弄一个html5音乐播放的网站,想让我的iphone和ipad爽一爽,前端采用jquery的一个插件jplayer,经过改造之后效果还不错。
后台采用php,定时采集百度的mp3。 考虑到本人服务器空间菊紧,当然只能采集mp3地址,文件并没有下载到本地。考虑到百度mp3路径经常变,实在是蛋疼,于是必须定时判断一下mp3路径还对不对,于是就有了php判断远程文件是否存在这篇软文。开始用get_headers() 方法,后来听说存在效率问题,于是不使用此解决方案,但是也顺带一提吧,下面看看get_headers函数的效果:
复制代码 代码如下:
//默认效果
print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif"));
结果:
array
(
[0] => http/1.1 200 ok
[1] => date: thu, 02 jun 2011 02:47:27 gmt
[2] => server: apache
[3] => p3p: cp=" oti dsp cor iva our ind com "
[4] => set-cookie: baiduid=7f6a5a2ed03878a7791c89c526966f3a:fg=1; expires=fri, 01-jun-12 02:47:27 gmt; max-age=31536000; path=/; domain=.baidu.com; version=1
[5] => last-modified: thu, 20 jan 2011 07:15:35 gmt
[6] => etag: "65e-49a41e65933c0"
[7] => accept-ranges: bytes
[8] => content-length: 1630
[9] => cache-control: max-age=315360000
[10] => expires: sun, 30 may 2021 02:47:27 gmt
[11] => connection: close
[12] => content-type: image/gif
)
//加参数1的效果
print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif", 1));
结果:
array
(
[0] => http/1.1 200 ok
[date] => thu, 02 jun 2011 02:49:28 gmt
[server] => apache
[p3p] => cp=" oti dsp cor iva our ind com "
[set-cookie] => baiduid=4d875812fc482c0ade4f5c17068849ee:fg=1; expires=fri, 01-jun-12 02:49:28 gmt; max-age=31536000; path=/; domain=.baidu.com; version=1
[last-modified] => thu, 20 jan 2011 07:15:35 gmt
[etag] => "65e-49a41e65933c0"
[accept-ranges] => bytes
[content-length] => 1630
[cache-control] => max-age=315360000
[expires] => sun, 30 may 2021 02:49:28 gmt
[connection] => close
[content-type] => image/gif
)
怎么样,get_headers函数还是不错的吧,不过既然效率有问题,那只好不优先考虑了,curl就不错,下面看看curl的做法
复制代码 代码如下:
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://www.baidu.com/img/baidu_sylogo1.gif');
echo $exists ? '存在' : '不存在';
$exists = check_remote_file_exists('http://www.baidu.com/test.jpg');
echo $exists ? '存在' : '不存在';
0
0
相关文章
如何在 PHP 中基于当前时间智能筛选并显示最近的广播节目安排
php打包exe怎么设置管理员权限_提权运行设置技巧【说明】
php485在linux下权限不够怎么办_php485串口权限配置教程【技巧】
PHP 实现电台节目单的智能时间匹配与动态展示
如何使用PHP根据用户会话导出专属SQL数据到Excel文件
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!
7
2025.12.31
想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。
4
2025.12.31
想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!
2
2025.12.31
热门下载
精品课程
最新文章







