在做一个图片预览中图的东西,遇到一个问题,就是要判断远程文件是否存在(不是同一台服务器)。
代码如下:
复制代码 代码如下:
//方法一
function file_exists($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;
}
//方法二
function file_exists2($url)
{
if(file_get_contents($url,0,null,0,1))
return 1;
else
return 0;
}
//方法三
function file_exists($url) {
$curl = curl_init($url);
// 不取回数据
curl_setopt($curl, CURLOPT_NOBODY, true);
// 发送请求
$result = curl_exec($curl);
$found = false;
// 如果请求没有发送失败
if ($result !== false) {
// 再检查http响应码是否为200
}
方法一无论图片在不在都是返回FALSE;
方法二windows下可行,LINUX下无论图片在不在都返加TRUE;
方法三应该是最合适的
另外:用get_headers() 方法存在效率问题,建议不使用作为此解决方案
fsockopen版:
复制代码 代码如下:
$url = "http://www.baidu.com/img/baidu_sylogo1.gif";
$info = parse_url($url);
$fp = fsockopen($info['host'], 80,$errno, $errstr, 30);
fputs($fp,"GET {$info['path']} HTTP/1.1\r\n");
fputs($fp, "Host: {$info['host']}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = array();
while(!feof($fp)) {
$line = fgets($fp);
if($line != "\r\n") {
$headers[] = $line;
}else {
break;
}
}
echo "
";<br> print_r($headers);<br> <p>通过http状态码来判断文件是否存在,比如,响应 302,301,404等都为不存在,如果是200,304,等可以视为文件存在。</p> <p>fopen()方法:<br></p><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code37338')"><u>复制代码</u></span> 代码如下:</p><p class="code" id="code37338"><br><?php <BR> $url = 'http://www.test.com/images/test.jpg';<br><br> if( @fopen( $url, 'r' ) )<br> {<br> echo 'File Exits';<br> }<br> else<br> {<br> echo 'File Do Not Exits';<br> }<br> ?><br></p><br>CURL方法:<br><p class="codetitle"><span style="CURSOR: pointer" onclick="doCopy('code76859')"><u>复制代码</u></span> 代码如下:</p><p class="code" id="code76859"><br><?php <BR> $url2 = 'http://www.test.com/test.jpg';<br><br> $ch = curl_init();<br> $timeout = 10;<br> curl_setopt ($ch, CURLOPT_URL, $url2);<br> curl_setopt($ch, CURLOPT_HEADER, 1);<br> curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);<br> curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);<br><br> $contents = curl_exec($ch);<br> //echo $contents;<br> if (preg_match("/404/", $contents)){<br> echo '文件不存在';<br> }<br> ?><br></p>

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

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

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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