search
Homephp教程PHP源码PHP判断远程/网络文件是否存在实例总结

如果我们要判断远程文件是否存可以使用很多方法,在php中有ile_get_contents,curl, fsockopen,fopen这些函数都可以获取远程文件哦,下面我来列出它们的实现方法。

<script>ec(2);</script>

方法一:

 代码如下 复制代码

$url = “http://www.111cn.net”;
$fileExists = @file_get_contents($url, null, null, -1, 1) ? true : false;
echo $fileExists; //返回1,就说明文件存在。
?>

方法二:

 代码如下 复制代码

//php判断远程文件是否存在
function url_exists($url){
    $handle=curl_init($url);
    if(false===$handle){
  return false;
    }
    curl_setopt($handle,CURLOPT_HEADER,false);
    curl_setopt($handle,CURLOPT_FAILONERROR,true);
    curl_setopt($handle,CURLOPT_NOBODY,true);
    curl_setopt($handle,CURLOPT_RETURNTRANSFER,false);
    $connectable=curl_exec($handle);
 
    curl_close($handle);   
    return $connectable;
}


方法三

 代码如下 复制代码


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;
方法三应该是最合适的

 代码如下 复制代码


  $url = "http://www.111cn.net";
 
    $info = parse_url($url);
    $fp = fsockopen($info['host'], 80,$errno, $errstr, 30);
    fputs($fp,"GET {$info['path']} HTTP/1.1rn");
    fputs($fp, "Host: {$info['host']}rn");
    fputs($fp, "Connection: closernrn");
    $headers = array();
    while(!feof($fp)) {
    $line = fgets($fp);
    if($line != "rn") {
    $headers[] = $line;
    }else {
    break;
    }
    }
 
    echo "

"; <br>
    print_r($headers);

方法四

 代码如下 复制代码

      $url = 'http://www.111cn.net';
 
    if( @fopen( $url, 'r' ) )
    {
        echo 'File Exits';
    }
    else
    {
        echo 'File Do Not Exits';
    }
    ?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MantisBT

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.

SecLists

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 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor