How to get the size of a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thatonejewishguy
Forum Newbie
Posts: 4
Joined: Sun Sep 11, 2005 11:45 am

How to get the size of a file

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
thatonejewishguy
Forum Newbie
Posts: 4
Joined: Sun Sep 11, 2005 11:45 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
thatonejewishguy
Forum Newbie
Posts: 4
Joined: Sun Sep 11, 2005 11:45 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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']
thatonejewishguy
Forum Newbie
Posts: 4
Joined: Sun Sep 11, 2005 11:45 am

Post by thatonejewishguy »

awesome thanks for the help
Post Reply