php判断路径是否存在域名,PHP检查文件是否在不同域名上的存在情况

PHP检查文件是否在不同域名下的存在情况

Earlier today I needed to find out if a file exists on a different domain. Initially I used the file_exists

function, but then when that threw back an error I remembered that

file_exists only checks whether a file or directory exists on the same

server as the script.

After I played around with various functions, I came up with a few lines of code that actually works:

How to check if file exists on a different domain

$image = "http://www.example.co.uk/images/1.jpg";

$handle = @fopen("$image", "r");

if(strpos($handle, "Resource id") !== false)

{

echo "file does exist";

}

else

{

echo "file does not exist";

}

?>

?

The logic explained

Ok, so if the file exists (1.jpg) the fopen function will throw back a

“resource id” response. So I check the response to see if “response id”

exists with the strpos function. It’s really as simple as that.

I’m not entirely sure if my method is the best, nor the most

efficient, but it seems to work pretty well, and I can’t think of any

other methods. Anyone know of any other/better methods?

Better solution

Thanks to a comment left by Paul I’ve been made aware of a better solution.

?

$url = "http://www.example.com/index.php";

$header_response = get_headers($url, 1);

if ( strpos( $header_response[0], "404" ) !== false )

{

// FILE DOES NOT EXIST

}

else

{

// FILE EXISTS!!

}

?

?

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值