gpt4 book ai didi

PHP 使用 file_get_contents() 检查外部服务器上是否存在文件

转载 作者:可可西里 更新时间:2023-11-01 00:44:03 25 4
gpt4 key购买 nike

使用 file_get_contents() 方法检查外部服务器上是否存在文件,此方法是否可以正常工作?

$url_file = "http://website.com/dir/filename.php";
$contents = file_get_contents($url_file);

if($contents){
echo "File Exists!";
} else {
echo "File Doesn't Exists!";
}

最佳答案

我认为对我来说最好的方法是使用这个脚本:

$file = "http://website.com/dir/filename.php";
$file_headers = get_headers($file);

如果文件不存在,$file_headers[0] 的输出是:HTTP/1.0 404 Not FoundHTTP/1.1 404 Not Found

如果文件不存在,使用 strpos 方法检查 404 字符串:

if(strpos($file_headers[0], '404') !== false){
echo "File Doesn't Exists!";
} else {
echo "File Exists!";
}

感谢所有帮助:)

关于PHP 使用 file_get_contents() 检查外部服务器上是否存在文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25355251/

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