gpt4 book ai didi

php - 检查文件是否存在于另一个网站上

转载 作者:搜寻专家 更新时间:2023-10-31 20:35:25 25 4
gpt4 key购买 nike

我一直在尝试开发代码来检查另一个网站上是否存在 pdf 文件。出于测试目的,我在网上找到了一个随机的 pdf 文件:

http://www.tutorialspoint.com/php/php_tutorial.pdf

我尝试了以下代码,但两种方法均无效:

方法一:

$path1 = 'http://www.tutorialspoint.com/php/php_tutorial.pdf';
if (file_exists($path1))
{
echo "found!";
}
else
{
echo "not found";
}

//RESULT: not found

方法二:

function UR_exists($url){
$headers=get_headers($url);
return stripos($headers[0],"200 OK")?true:false;
}

if(UR_exists('http://www.tutorialspoint.com/php/php_tutorial.pdf'))
echo "This page exists";
else
echo "This page does not exist";

//RESULT: This page does not exist

页面在这两种情况下都执行得很好,但结果总是文件不存在,而我知道它存在,哈哈。我做错了什么?

最佳答案

file_exists 使用物理路径,您需要提供的参数应该是可以找到该文件的服务器上的地址,而不是 url!另一方面, header 方法应该可以正常工作!但是针对 404 header 响应进行测试是值得尝试的,您可以这样做:

$url = "http://www.tutorialspoint.com/php/php_tutorial.pdf";
$header_response = get_headers($url);
if (header_response) {
if ( strpos( $header_response[0], "404" ) !== false ){
// PDF DOES NOT EXIST
echo "PDF DOES NOT EXIST";
}else{
// PDF EXISTS!!
echo "PDF EXISTS";
}
}else {
echo "PDF DOES NOT EXIST";
}

请记住,应该启用允许您使用外部 URL 的allow_url_fopen = 1

关于php - 检查文件是否存在于另一个网站上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36825220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com