Page 1 of 1

How to get the size of a file

Posted: Sun Sep 11, 2005 11:49 am
by thatonejewishguy
I know how to get the size of a file that is in the same directory but what if it is a couple folders away the code that I uses which is

Code: Select all

<?php

// outputs e.g.  somefile.txt: 1024 bytes

$filename = 'db.php';
echo $filename . ': ' . filesize($filename) . ' bytes';

?>
doesnt work.
any ideas?

Posted: Sun Sep 11, 2005 11:53 am
by feyd
filesize() needs to know how to find the file so you can either use relative pathing or absolute paths. An absolute path would start with /, while a relative would either have dot, double-dot, or anything non-forward slash depending on it's location relative to the current directory, parent directory or a child directory (respectively)

Posted: Sun Sep 11, 2005 1:57 pm
by thatonejewishguy
feyd wrote:filesize() needs to know how to find the file so you can either use relative pathing or absolute paths. An absolute path would start with /, while a relative would either have dot, double-dot, or anything non-forward slash depending on it's location relative to the current directory, parent directory or a child directory (respectively)
can i get an example? :-D

Posted: Sun Sep 11, 2005 2:13 pm
by feyd
  • Local File:

    Code: Select all

    filesize('foo.ext');
  • Current Directory:

    Code: Select all

    filesize('./foo.ext'); // basically the same as Local File
  • Parent Directory:

    Code: Select all

    filesize('../foo.ext'); // foo.ext one directory up from the current working directory
  • Child Directory:

    Code: Select all

    filesize('bar/foo.ext'); // bar is a directory under the current working directory
each of the path modifers can be combined. Example:

Code: Select all

filesize('../../bar/baf/foo.ext'); // two directories up, in a child directory baf of a child directory bar from the current working directory

Posted: Sun Sep 11, 2005 2:23 pm
by thatonejewishguy

Code: Select all

<?php

// outputs e.g.  somefile.txt: 1024 bytes

$directory = '/files/video/files/';
$filename = 'BetterJS.wmv';
$fileloc = $directory . $filename;
echo $filename . ': ' . filesize($fileloc) . ' bytes';

?>
Doesn't work.

see

Posted: Sun Sep 11, 2005 2:27 pm
by feyd
/files is a root level folder reference, I'd be willing to bet it doesn't exist. :) If you know where the file is from the webroot, use $_SERVER['DOCUMENT_ROOT']

Posted: Sun Sep 11, 2005 2:48 pm
by thatonejewishguy
awesome thanks for the help