tags:

views:

31

answers:

2

Is there a way in php to check remote files size and date/time on the server ?

A: 

You could use filemtime() and filesize().

Be aware of this BTW:

As of PHP 5.0.0, this function can also be used with some URL wrappers. 
Refer to List of Supported Protocols/Wrappers for a listing of which wrappers 
support stat() family of functionality.
Dennis Haarbrink
Thank you, I know that I can use those functions and I do locally. But how does one use this with remote server files ? I know its possible, just not at my level of knowledge just yet.
emcgfx
You just put in the url: `$last_modified_time = filemtime('http://server.com/file.php');` These methods do exactly the same as the one suggest by Yogesh. I would stick with simplicity though, this is a core functionality which has been thoroughly tested. Plus, with these methods you don't depend on the cURL lib.
Dennis Haarbrink
@ Dennis Haarbrink, and for file sieze same ? Between your method did not work :)
emcgfx
@emcgfx: Indeed it works the same for filesize: `$filesize = filesize('http://server.com/file.php');` What exactly didn't work? Did you get errors? What was the result of the function?
Dennis Haarbrink
@ Dennis Haarbrink, nothing at all.
emcgfx
@ Dennis H. getimagessize worked but slower then CURL. I need to be able to use curl any ways. Just strange that it did not return any values.
emcgfx
+3  A: 

try for date

function GetRemoteLastModified( $uri )
{
    // default
    $unixtime = 0;

    $fp = fopen( $uri, "r" );
    if( !$fp ) {return;}

    $MetaData = stream_get_meta_data( $fp );

    foreach( $MetaData['wrapper_data'] as $response )
    {
        // case: redirection
        if( substr( strtolower($response), 0, 10 ) == 'location: ' )
        {
            $newUri = substr( $response, 10 );
            fclose( $fp );
            return GetRemoteLastModified( $newUri );
        }
        // case: last-modified
        elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' )
        {
            $unixtime = strtotime( substr($response, 15) );
            break;
        }
    }
    fclose( $fp );
    return $unixtime;
}

and for file size

function remotefilesize($remoteFile){
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
  echo 'cURL failed';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
  $status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
}

Please check for explaination. I have got above functions from these links.

http://php.net/manual/en/function.filemtime.php

http://php.net/manual/en/function.filesize.php

Yogesh
WOW, Big Thank you Yogesh. Checking now :-)
emcgfx
Amazing worked very well and very fast. Just what I've been looking for. Now how would I edit this to be more user friendly.
emcgfx
@emcgfx : can you give me little more explanation , about your requirement?
Yogesh
@ Yogesh, how would I format files size to be more user friendly now ?
emcgfx
@ Yogesh, can first function GetRemoteLastModified( $uri ) be modified to also grab the file date ? Since its grabbing data from meta. Thanks
emcgfx
for file size try, function getsize ($size) { $si = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $si[$i]; }
Yogesh