4

http://example.com/file.txt phpにURLが存在するかどうかを確認したい 。どうすればいいですか?

4

5 に答える 5

4
if(! @ file_get_contents('http://www.domain.com/file.txt')){
  echo 'path doesn't exist';
}

これが最も簡単な方法です。に慣れていない場合は@、エラーがスローされた場合にfalseを返すように関数に指示します。

于 2012-11-05T05:30:51.607 に答える
3

は、PHP の curl 拡張機能を使用します。

$ch = curl_init();                                  // set up curl
curl_setopt( $ch, CURLOPT_URL, $url );              // the url to request
if ( false===( $response = curl_exec( $ch ) ) ){    // fetch remote contents
    $error = curl_error( $ch );                  
    // doesn't exist
}
curl_close( $ch );                                  // close the resource
于 2012-11-05T05:31:41.067 に答える
0
$filename="http://example.com/file.txt";    

if (file_exists($filename)) {
   echo "The file $filename exists";
} else {
   echo "The file $filename does not exist";
} 

また

if (fopen($filename, "r"))
{
   echo "File Exists"; 
}
else
{
   echo "Can't Connect to File";
}
于 2012-11-05T05:32:17.690 に答える
0

この関数をPing サイトで試し、結果を PHP で返します。

function urlExists($url=NULL)  
    {  
        if($url == NULL) return false;  
        $ch = curl_init($url);  
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $data = curl_exec($ch);  
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
        curl_close($ch);  
        if($httpcode>=200 && $httpcode<300){  
            return true;  
        } else {  
            return false;  
        }  
    }
于 2012-11-05T05:33:33.977 に答える