我为 WordPress 创建了一个插件,它需要存在两个文件才能正常运行。
第一个文件定义为文件系统路径,第二个文件定义为 URL。
假设第一个文件是:
/home/my_site/public_html/some_folder/required_file.php
第二个文件是:
http://www.my_site.com/some_folder/required_url_file.php
注意这两个文件不是同一个文件放入文件系统中的。 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;
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 和文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7952977/