Log in

View Full Version : filetime() problems



liamallan
04-21-2011, 07:48 AM
hi, im trying to get the 'last modified' time of a file on my server, im currently using the filetime() function to try accomplish this. however, when i run the script, i get met with this error:

Warning: filemtime() [function.filemtime]: stat failed for http://mydomain.co.cc/path/to/file/filetogetsizeof.zip in /home/shifty/public_html/modules/downloads/downloads.php on line 144

this is the script i am running to try get the last modified time of the file:

$filelocation = ''.uhome().'/modules/downloads/downloads/'.$info['download_file'].'';
$last_modified = time() - filemtime($filelocation);

if(round($last_modified/(24*3600)) >= 1){ // over a day?
$last_modified /= (24*3600);
if(round($last_modified) > 6){ // at least a week?
$last_modified /= 7;
if(round($last_modified) > 3){ // .. months?
$last_modified /= 4;
if(round($last_modified) > 11){
$last_modified /= 12;
$output = round($last_modified).' years';
}
else{
$output = '~'.round($last_modified).' months';
}
}
else{
$output = round($last_modified).' weeks';
}
}
else{
$output = round($last_modified).' days';
}
}
else if(round($last_modified/3600) >= 1){ // At least one hour ago
$output = round($last_modified/3600).' hours';
}
else{
if($last_modified/60 < 1){ // barely a few secs ago
$output = $last_modified.' seconds';
}
else{
$output = round($last_modified/60).' minutes';
}
}
if(preg_match('/^1[^0-9]/', $output)){
$output = substr($output, 0, -1);
}
section_content(''.$output.' ago');

for every different file, i get the same output: 45 years ago

if anyone could give me any advice, or notice where im going wrong, i would be most greatful

traq
04-22-2011, 02:41 AM
I get that error when I try to run filemtime() on a non-existent file. Are you sure your file path is correct?

Beverleyh
04-22-2011, 06:00 AM
Also check that all file paths are absolute (start with '/home/www/mysite/...' rather than 'http://mysite/') as that may also be causing problems.

traq
04-22-2011, 02:23 PM
true, I didn't notice that. filesystem functions work locally on the server; not via the web. Your uhome() function seems to be returning a protocol and url.

traq
08-05-2011, 11:05 PM
no; she means that the file paths need to use a local path, not a web-based protocol and URL.

this:
http:// mysite.com/somedirectory/somefile.html
tries to request a file over the internet: i.e., using the http:// protocol.
filesystem functions do not use the internet to access files; they access files on the server. $_SERVER['DOCUMENT_ROOT'] should be configured with the server path to your website's home directory, so using the following provides the proper path with most web hosts:
$_SERVER['DOCUMENT_ROOT'] . '/somedirectory/somefile.html'
...which will actually produce something like /users/mysite/public_html/somedirectory/somefile.html