如何知道某个资源是否存在?

For instance, I want to know if these files exists.

http://www.stackoverflow.com/favicon.ico
http://www.stackoverflow.com/reset.css

and then download it ( if exixts obviously ).

<?php

if( ( $file = file_get_contents( 'http://www.stackoverflow.com/favicon.ico' ) ) ) {
    echo "file exists.";
    file_put_contents( 'favicon.ico', $file );
}
else {
    echo "File does not exist.";
}

Try this:

<?php
$ch = curl_init();

$url    = 'YOUR_URL_HERE'; // the url you want to check

curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER[ 'HTTP_USER_AGENT' ] );

// make "HEAD" request
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_NOBODY, true );

$res    = curl_exec( $ch );
$res    = explode( ' ', substr( $res, 0, strpos( $res, "
" ) ) );

// if 404, file does not exist
if( $res[ 1 ] != 404 ) {
    $file = file_get_contents( $url ); 
} else {
    // This url does not exist
    $file = '';
}

curl_close( $ch );
?>

Hope this helps.