PHP判断远程文件是否存在的几种方法
在做一个图片预览中图的东西,遇到一个问题,就是要判断远程文件是否存在(不是同一台服务器)。
代码如下:
0102030405060708091011121314151617181920212223242526272829303132333435 //方法一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;elsereturn
false;} //方法二function
file_exists2($url){if(file_get_contents($url,0,null,0,1))return
1;elsereturn
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版:
01020304050607080910111213141516171819 $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
"
"; print_r($headers); <p> </p> <p>通过http状态码来判断文件是否存在,比如,响应 302,301,404等都为不存在,如果是200,304,等可以视为文件存在。</p> <p> </p> <p>fopen()方法:</p> <p> </p> <p>010203040506070809101112 <?php $url</p> </p><p> </p> <p>= 'http://www.test.com/images/test.jpg'; if(</p> <p> @fopen(</p> <p>$url,</p> <p>'r'</p> <p> </p> <p>) ) { echo</p> <p> </p> <p>'File Exits'; } else { echo</p> <p> </p> <p>'File Do Not Exits'; } ?> </p> <p> </p> <p>CURL 方法:</p> <p> </p> <p>01020304050607080910111213141516 <?php $url2</p> </p><p> </p> <p>= 'http://www.test.com/test.jpg'; $ch</p> <p> </p> <p>= curl_init(); $timeout</p> <p> </p> <p>= 10; curl_setopt</p> <p> ($ch,</p> <p> CURLOPT_URL, $url2); curl_setopt($ch,</p> <p> CURLOPT_HEADER, 1); curl_setopt</p> <p> ($ch,</p> <p> CURLOPT_RETURNTRANSFER, 1); curl_setopt</p> <p> ($ch,</p> <p> CURLOPT_CONNECTTIMEOUT, $timeout); $contents</p> <p> </p> <p>= curl_exec($ch); //echo</p> <p> $contents; if</p> <p> </p> <p>(preg_match("/404/",</p> <p>$contents)){ echo</p> <p> </p> <p>'文件不存在'; } </p> <p> </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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.