PHP,检查URL和文件是否存在? [英] PHP, Check if URL and a file exists ?
本文介绍了PHP,检查URL和文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为WordPress创建了一个插件,要求存在两个文件才能正常运行。
第一个文件被定义为文件系统路径,第二个文件被定义为一个URL。
假设第一个文件是:
/home/my_site/public_html/some_folder/required_file.php
第二个文件是那:
http://www.my_site.com/some_folder/required_url_file.php
$ c $请注意,这两个文件不是相同的文件到文件系统。 required_file.php的内容不是required_url_file.php,它们的作用是绝对不同的。
关于如何验证两个文件存在的想法?
解决方案您可以同时检查:
$ file ='/home/my_site/public_html/some_folder/required_file.php';
$ url ='http://www.my_site.com/some_folder/required_url_file.php';
$ fileExists = is_file($ file);
$ urlExists = is_200($ url);
$ bothExists = $ fileExists&& $ urlExists;
函数is_200($ url)
{
$ options ['http'] = array(
'method'=>HEAD,
'ignore_errors'=> 1,
'max_redirects'=> 0
);
$ body = file_get_contents($ url,NULL,stream_context_create($ options));
sscanf($ http_response_header [0],'HTTP /%* d。%* d%d',$ code);
返回$ code === 200;
}
I create a plugin for WordPress that requires two files to be exists in order to operate normaly.
The first file is defined as a file system path and the second file is defined as a URL.
Let's say the first file is that:
/home/my_site/public_html/some_folder/required_file.php
and the second file is that:
http://www.my_site.com/some_folder/required_url_file.php
Note that both files are not the same file into the file system. The required_file.php has other content than the required_url_file.php and they act absolutly diferent
Any idea on how to validate the existance of both files ?
解决方案 You can check both:
$file = '/home/my_site/public_html/some_folder/required_file.php';
$url = 'http://www.my_site.com/some_folder/required_url_file.php';
$fileExists = is_file($file);
$urlExists = is_200($url);
$bothExists = $fileExists && $urlExists;
function is_200($url)
{
$options['http'] = array(
'method' => "HEAD",
'ignore_errors' => 1,
'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);
return $code === 200;
}
这篇关于PHP,检查URL和文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文