亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網(wǎng)絡(luò),我們將竭誠為您提供優(yōu)質(zhì)的服務(wù)! 誠征網(wǎng)絡(luò)推廣 | 網(wǎng)站備案 | 幫助中心 | 軟件下載 | 購買流程 | 付款方式 | 聯(lián)系我們 [ 會(huì)員登錄/注冊 ]
促銷推廣
客服中心
業(yè)務(wù)咨詢
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352289
點(diǎn)擊這里給我發(fā)消息  81721488
有事點(diǎn)擊這里…  376585780
有事點(diǎn)擊這里…  872642803
有事點(diǎn)擊這里…  459248018
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  380791050
技術(shù)支持
有事點(diǎn)擊這里…  714236853
有事點(diǎn)擊這里…  719304487
有事點(diǎn)擊這里…  1208894568
有事點(diǎn)擊這里…  61352289
在線客服
有事點(diǎn)擊這里…  531199185
有事點(diǎn)擊這里…  61352288
有事點(diǎn)擊這里…  983054746
有事點(diǎn)擊這里…  893984210
當(dāng)前位置:首頁 >> 技術(shù)文章 >> 文章瀏覽
技術(shù)文章

PHP判斷遠(yuǎn)程文件是否存在的幾種方法

添加時(shí)間:2013-12-19 16:52:35  添加: 思海網(wǎng)絡(luò) 

 在做一個(gè)圖片預(yù)覽中圖的東西,遇到一個(gè)問題,就是要判斷遠(yuǎn)程文件是否存在(不是同一臺服務(wù)器)。

  代碼如下:

  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);//

  不取回?cái)?shù)據(jù)curl_setopt($curl,

  CURLOPT_NOBODY, true);//

  發(fā)送請求$result

  = curl_exec($curl);$found

  = false;//

  如果請求沒有發(fā)送失敗if

  ($result

  !== false) {//

  再檢查http響應(yīng)碼是否為200}

  方法一無論圖片在不在都是返回FALSE;

  方法二windows下可行,LINUX下無論圖片在不在都返加TRUE;

  方法三應(yīng)該是最合適的

  另外:用get_headers() 方法存在效率問題,建議不使用作為此解決方案

  fsockopen版:

  01020304050607080910111213141516171819   $url

  = "

  .baidu./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

  "<pre>";    print_r($headers);

  通過http狀態(tài)碼來判斷文件是否存在,比如,響應(yīng) 302,301,404等都為不存在,如果是200,304,等可以視為文件存在。

  fopen()方法:

  010203040506070809101112   <?php    $url

  = test./images/test.jpg';     if(

  @fopen(

  $url,

  'r'

  ) )    {        echo

  'File Exits';    }    else    {        echo

  'File Do Not Exits';    }    ?>

  CURL 方法:

  01020304050607080910111213141516 <?php    $url2

  = test./test.jpg';     $ch

  = curl_init();    $timeout

  = 10;    curl_setopt

  ($ch,

  CURLOPT_URL, $url2);    curl_setopt($ch,

  CURLOPT_HEADER, 1);    curl_setopt

  ($ch,

  CURLOPT_RETURNTRANSFER, 1);    curl_setopt

  ($ch,

  CURLOPT_CONNECTTIMEOUT, $timeout);     $contents

  = curl_exec($ch);    //echo

  $contents;    if

  (preg_match("/404/",

  $contents)){        echo

  '文件不存在';    }

關(guān)鍵字:PHP、遠(yuǎn)程文件、圖片預(yù)覽

分享到:

頂部 】 【 關(guān)閉
版權(quán)所有:佛山思海電腦網(wǎng)絡(luò)有限公司 ©1998-2024 All Rights Reserved.
聯(lián)系電話:(0757)22630313、22633833
中華人民共和國增值電信業(yè)務(wù)經(jīng)營許可證: 粵B1.B2-20030321 備案號:粵B2-20030321-1
網(wǎng)站公安備案編號:44060602000007 交互式欄目專項(xiàng)備案編號:200303DD003  
察察 工商 網(wǎng)安 舉報(bào)有獎(jiǎng)  警警  手機(jī)打開網(wǎng)站