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

PHP,检查URL和文件是否存在?,php,wordpress,filesystems,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 第二个文件是: /home/my_site/public_html/some_folder/required_file.php

我为WordPress创建了一个插件,它需要两个文件才能正常运行

第一个文件定义为文件系统路径,第二个文件定义为URL

假设第一个文件是:

/home/my_site/public_html/some_folder/required_file.php
http://www.my_site.com/some_folder/required_url_file.php
第二个文件是:

/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之外还有其他内容,它们的行为完全不同


您知道如何验证两个文件的存在吗?

要检查文件是否存在,请使用以下方法

从PHP5.0.0开始,此函数还可以与someURL一起使用 包装纸。请参阅以确定 包装器支持stat()系列功能

如果您有可用的PECL函数,您可以检查它是否返回远程文件的状态代码200

要检查您是否可以访问本地文件,可以使用
文件\u exists
,但这并不意味着您将能够访问该文件。要检查是否可以读取该文件,请使用
是否可读

使用函数文件\u exists()


将得到正确或错误的结果。

检查文件是否存在:

if (file_exists('path/to/file.txt')) {
    echo "File exists!";
} else {
    echo "File doesn't exist.";
}
$data = @file_get_contents("http://url.com/");
if (!$data) {
    echo "URL not valid.";
} else {
    echo "URL is valid.";
}
检查URL是否有效:

if (file_exists('path/to/file.txt')) {
    echo "File exists!";
} else {
    echo "File doesn't exist.";
}
$data = @file_get_contents("http://url.com/");
if (!$data) {
    echo "URL not valid.";
} else {
    echo "URL is valid.";
}
注意事项:

if (file_exists('path/to/file.txt')) {
    echo "File exists!";
} else {
    echo "File doesn't exist.";
}
$data = @file_get_contents("http://url.com/");
if (!$data) {
    echo "URL not valid.";
} else {
    echo "URL is valid.";
}
理想情况下,您不应该尝试预测文件系统。虽然诸如file_exists之类的方法非常有用,但不应依赖它们,相反,您应该尝试写入文件、读取文件等,然后捕获并处理发生的任何异常或错误。

您可以同时检查:

$file_exists = file_exists($path);
$url_accessable = http_get($url, array("timeout"=>10), $info); // should not be FALSE
$status_code = $info['response_code'] //should be 200
$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;
}

至于验证URL,这些答案都没有考虑执行此任务的正确WordPress方法

对于此任务,应使用
wp\u remote\u head()

这是我写的一篇文章。查看并了解其工作原理。

远程:

$file = 'http://www.my_site.com/some_folder/required_url_file.php'
if ( @fclose(@fopen($file,"r")) ) echo "File exists!";
本地:

$file = '/home/my_site/public_html/some_folder/required_file.php';
if ( is_file($file) ) echo "File exists!";

基于Maor H.代码示例,以下是我在插件中使用的函数:

/**
 * Check if an item exists out there in the "ether".
 *
 * @param string $url - preferably a fully qualified URL
 * @return boolean - true if it is out there somewhere
 */
function webItemExists($url) {
    if (($url == '') || ($url == null)) { return false; }
    $response = wp_remote_head( $url, array( 'timeout' => 5 ) );
    $accepted_status_codes = array( 200, 301, 302 );
    if ( ! is_wp_error( $response ) && in_array( wp_remote_retrieve_response_code( $response ), $accepted_status_codes ) ) {
        return true;
    }
    return false;
}
我已经在helper类中创建了这个方法,但是将它放在主题的functions.php文件中应该可以使它在任何地方都可以访问。但是,您应该始终在类中编写并实例化它们。隔离插件和主题功能会更好

有了此功能,您只需使用:

如果(webItemExists('http://myurl.com/thing.png){打印“它存在”;}


大多数情况下,您将使用WordPress调用通过相对或完全限定的URL访问所有项目。如果您对/uploads/2012/12/myimage.png等内容有相对引用,则可以将其转换为完全限定的URL v。只需添加get_site_URL()即可创建WordPress相对URL$调用webItemExists函数时使用字符串。

这似乎适用于我:

function url_file_exists($url) {
    $context  = stream_context_create(array('http' =>array('method'=>'HEAD')));
    $fd = @fopen($url, 'rb', false, $context);
    if ($fd!==false) {
       fclose($fd);
       return true;
    }
    return false;
}

可能重复的-供文件使用。您通常不希望依赖文件_exists,因为如果目录存在,它将返回正数。除非您非常确定返回的文件url和文件名是什么,否则当返回文件名时,目录通常会返回正数。Use is_file()。is_200()不是有效的WordPress函数。这段代码在PHP5.2上的WordPress3.3中失败。@CharlestonSoftwareAssociates:是谁写的,它是WordPress的一部分?如果从答案中复制代码,请不要忘记复制函数定义。;)好的,忽略了这一部分,但是这种方法仍然不是WordPress设置的最佳方法。首先,200不是存在远程URL项的唯一有效响应。其次,WordPress中的wp_remote_head()方法在file_get_内容之外有多个回退,包括curl和directsocket操作。我会删除否决票,因为你的答案会起作用,但不是WordPress项目的最佳解决方案。。。但它也被锁定了/你写的最后一点在技术上是正确的,然而,由于Wordpress是随用随付,什么是最好的不是问题,而是什么有效。这确实有效,所以没关系。我不会太依赖Wordpress的核心函数,因为函数堆栈非常古怪。更好的方法是创建自己的API,这样您就知道自己拥有什么了。如果功能体不起作用,可以用不同的东西替换。这是一个很好的解决方案。本文中提供的代码是这种类型测试的正确设置。我将在下面的“webItemExists()”函数中发布该代码的简化版本。我会在这里做,但是注释不适合代码共享。谢谢@charlestonsoftwareasociates!我很高兴你发现它很有用。似乎上面这篇文章的访问权限目前还不是公开的(该网站要求登录名和密码)。它将不起作用。我查过你的密码了。不工作。。如果(file_exists(“){return“file exists!”;}否则{return“file exists!”;}我的链接是wkring。你可以从谷歌查看任何链接。我测试了你的代码。这是一个条件。如果你得到200个表单URL**()**然后你的函数返回True。所以在这种情况下,你的文件的URL和你的服务器在任何情况下返回200,那么你将不会检查…所以,在这种情况下,你的方法是不正确的。