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

时间:2011-10-31 11:18:12

标签: 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之外的其他内容,并且它们的行为绝对不同

关于如何验证两个文件存在的任何想法?

10 个答案:

答案 0 :(得分:6)

您可以同时查看两者:

$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;
}

答案 1 :(得分:5)

基于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;
}

我已经在辅助类中创建了一个方法,但是将它放在主题的functions.php文件中应该可以使它在任何地方都可以访问。但是,您应始终在类中编写并实例化它们。隔离插件和主题功能要好得多。

有了这个,你可以简单地使用:

if(webItemExists('http://myurl.com/thing.png')){print'it iexists'; }

大多数情况下,您将使用WordPress调用通过相对或完全限定的URL访问所有项目。如果你有/uploads/2012/12/myimage.png之类的相对引用,你可以通过简单地添加get_site_url()将它们转换为完全限定的URL v。一个WordPress相对URL。调用webItemExists函数时的$ string。

答案 2 :(得分:4)

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

对于此任务,应使用wp_remote_head()

以下是我撰写的关于How To Check Whether an External URL Exists with WordPress’ HTTP API的文章。检查一下,弄清楚它是如何工作的。

答案 3 :(得分:2)

$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

答案 4 :(得分:1)

要检查文件是否存在,请使用file_exists方法。

  

从PHP 5.0.0开始,此功能也可以与某些 URL一起使用   包装。请参阅Supported Protocols and Wrappers以确定哪个   包装器支持stat()系列功能。

if(! (file_exists($url1) && file_exists($url2)) ) {
    die("Files don't exist - throw error here.");
}

// Continue as usual - files exist at this point.

答案 5 :(得分:1)

如果您有PECL http_head功能,则可以检查它是否返回远程文件的状态代码200。

要检查您是否可以访问本地文件,可以使用file_exists,但这并不表示您将能够访问该文件。要检查您是否可以阅读该文件,请使用is_readable

答案 6 :(得分:1)

使用函数file_exists()

file_exists('http://www.my_site.com/some_folder/required_url_file.php');

会将结果视为真或假。

答案 7 :(得分:1)

远程:

$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!";

答案 8 :(得分:1)

这似乎对我有用:

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;
}

答案 9 :(得分:0)

检查文件是否存在:

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这样的方法非常有用,但是不应该依赖它们,而应该尝试写入文件,从中读取等,然后捕获并处理发生的任何异常或错误。