我想查看网址
http://example.com/file.txt
是否存在于php中。我该怎么办?
答案 0 :(得分:3)
if(! @ file_get_contents('http://www.domain.com/file.txt')){
echo 'path doesn't exist';
}
这是最简单的方法。如果您不熟悉@
,则会指示函数返回false,否则会抛出错误
答案 1 :(得分:2)
将使用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
答案 2 :(得分:0)
$filename="http://example.com/file.txt";
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
OR
if (fopen($filename, "r"))
{
echo "File Exists";
}
else
{
echo "Can't Connect to File";
}
答案 3 :(得分:0)
在Ping site and return result in 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;
}
}
答案 4 :(得分:0)
我同意这个回应,我通过这样做取得了成功
$url = "http://example.com/file.txt";
if(! @(file_get_contents($url))){
return false;
}
$content = file_get_contents($url);
return $content;
您可以按照代码检查位置是否存在。