PHP, Check if URL and a file exists ?

2019-05-22 13:18发布

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 ?

10条回答
来,给爷笑一个
2楼-- · 2019-05-22 13:44

Use function file_exists()

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

will get you results as True or false.

查看更多
贪生不怕死
3楼-- · 2019-05-22 13:48

remote:

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

local:

$file = '/home/my_site/public_html/some_folder/required_file.php';
if ( is_file($file) ) echo "File exists!";
查看更多
爷、活的狠高调
4楼-- · 2019-05-22 13:49

This seems to work for me:

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;
}
查看更多
时光不老,我们不散
5楼-- · 2019-05-22 13:50
$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
查看更多
Anthone
6楼-- · 2019-05-22 13:51

Checking if a file exists:

if (file_exists('path/to/file.txt')) {
    echo "File exists!";
} else {
    echo "File doesn't exist.";
}

Checking if a URL is valid:

$data = @file_get_contents("http://url.com/");
if (!$data) {
    echo "URL not valid.";
} else {
    echo "URL is valid.";
}

Notes:

Ideally you shouldn't try and predict the filesystem. Whilst methods such as file_exists are very helpful, they shouldn't be relied upon and instead you should attempt to write to files, read from them, etc, and then catch and handle any exceptions or errors that occur.

查看更多
Anthone
7楼-- · 2019-05-22 13:53

Based on Maor H. code sample, here is a function I am using in my plugins:

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

I've made this a method in a helper class, however putting this in your theme's functions.php file should make it generally accessible everywhere. However you should always be writing in classes and instantiating them. It is much better for isolating your plugin and theme functionality.

With this in place you can simply use:

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

Most often you will be using WordPress calls to access all items via a relative or fully qualified URL. If you have a relative reference to something such as /uploads/2012/12/myimage.png you can convert those to a fully qualified URL v. a WordPress relative URL by simply adding get_site_url() . $string when calling the webItemExists function.

查看更多
登录 后发表回答