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
2010-09-17 11:26:02
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
2010-09-17 11:30:57
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
2010-09-17 11:45:02
@ Dennis Haarbrink, and for file sieze same ? Between your method did not work :)
emcgfx
2010-09-17 11:55:53
@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
2010-09-17 12:02:29
@ Dennis Haarbrink, nothing at all.
emcgfx
2010-09-17 12:05:31
@ 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
2010-09-17 12:06:52
+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.
Yogesh
2010-09-17 11:31:22
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
2010-09-17 11:36:05
@emcgfx : can you give me little more explanation , about your requirement?
Yogesh
2010-09-17 11:58:32
@ Yogesh, can first function GetRemoteLastModified( $uri ) be modified to also grab the file date ? Since its grabbing data from meta. Thanks
emcgfx
2010-09-17 12:09:24
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
2010-09-17 12:24:30