PHP,检查 URL 和文件是否存在?

标签 php wordpress filesystems

我为 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/

相关文章:

php - 如何识别正确的电子邮件以更新正确的数据库记录?

phpMyAdmin - mySQL - 将数据导入 WordPress 时出错 - 什么是排序规则?

javascript - 如何在短代码中使用 javascript 变量?

jquery - 升级后子菜单未出现在 WordPress 网站上

php - 测试一个目录是否是另一个文件夹的子目录

perl - 如何在 Perl 中找到文件的大小写保留名称?

javascript - 如何限制表单提交后的每次提交次数

php - Symfony 2 注销

filesystems - Amazon S3文件分区最佳实践

php - 使用 Wordpress '不能使用 stdClass 类型的对象作为数组'