Welcome to WebmasterWorld Guest from 184.73.3.107

Forum Moderators: coopster & jatar k

Can get filesize! help

   
11:19 am on Mar 22, 2012 (gmt 0)

5+ Year Member



hi, i can't get filesize even though i can retrieve the name of my file. I tried this simple function $size = filesize("$file") but all it showed the name of the file but empty filesize.
Please help



function createDir($path = '.')
{
if ($handle = opendir($path))
{
echo "<ul>";

$files_array = array();
while (false !== ($files_array[] = readdir($handle)));
array_pop($files_array);
sort($files_array, SORT_STRING);
foreach($files_array as $file)

{
if (is_dir($path.$file) && $file != '.' && $file !='..')
printSubDir($file, $path, $queue);
else if ($file != '.' && $file !='..')
$queue[] = $file;
}

printQueue($queue, $path);

echo "</ul>";
}
}

function printQueue($queue, $path)
{
$i=1;

foreach ($queue as $file)
{
printFile($i, $file, $path);
$i++;
}
}

function printFile($i, $file, $path)
{

echo "<li><img src='images/doc.jpg'>&nbsp;<font class='list'>$i</font> - <a href=\"".$path.$file."\" target=\"new\"><img src='view.php?src=".$path.$file."&w=180&q=100&s=1&f=11' align=\"absmiddle\"></a> | <a href=\"../includes/delete.php?file=".$path.$file."&amp;delete=true\" class=\"button red medium\">Delete This Image</a>

</li>";

}


3:46 pm on Mar 22, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



There is a difference between the full system path and a URL. Many PHP coders work "relatively," that is, "relative to wherever you are at this moment." This often works, but sometimes doesn't, which is where I think you are now.

When you're doing file operations, use the full path to root: $_SERVER['DOCUMENT_ROOT']. When you're doing URL's, use the domain's root, '/'.

When you read a directory, it gets the file name, but it doesn't get the full path to that file. Assuming "path" is just some directory, like "myfiles," try

function printFile($i, $file, $path)
{
$fullpath = $_SERVER['DOCUMENT_ROOT'] . '/' . $path . $file;
$fsize = filesize($fullpath);

echo "<li><img src=\"/images/doc.jpg\">&nbsp;<font class=\"list\">$i</font> - <a href=\"/".$path.$file."\" target=\"new\"><img src=\"/view.php?src=".$path.$file."&amp;w=180&amp;q=100&amp;s=1&amp;f=11' align=\"absmiddle\"></a> (size: $fsize) | <a href=\"/includes/delete.php?file=".$path.$file."&amp;delete=true\" class=\"button red medium\">Delete This Image</a>
</li>";

}

Note the leading / before $path (and throughout your code) in the URL. Always start at domain root for URL's. Use full system paths for file operations. Print $fullpath to the browser to debug.
1:13 am on Mar 23, 2012 (gmt 0)

5+ Year Member



hi rocknbil

im getting error msg using ur coding

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/domain/#*$!x/#*$!/

i did tried to get using full path
filesize(/home/domain/#*$!/01.jpg) but it still echoing the file name but empty size.

Please help
1:53 am on Mar 23, 2012 (gmt 0)

5+ Year Member



hi rocknbil,

i cant seem to be able to get the filesize inside the function
function printFile($i, $file, $path) as it does increment the files inside my folder but i if i test the coding outside the function eg: $fsize = filesize('../folder/01.jpg');
echo "$fsize"; it works but it only get 1 file sizes and this is not what i want.

How do u get the correct coding inside function printFile($i, $file, $path)
8:23 am on Mar 23, 2012 (gmt 0)

5+ Year Member



hi rocknbil,

Regarding to my early reply error msg: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/domain/#*$!x/#*$!/. i have miss out the semicolon at the end of your coding but i still can't get the filesize and instead a Warning error


Warning: filesize() [function.filesize]: stat failed for /home/domain/public_html/site_com/../folder/CIB_1.jpg in /home/domain/public_html/site_com/admin/script.php on line 183
2:05 pm on Mar 23, 2012 (gmt 0)

5+ Year Member



hi rocknbil,

i have sorted out the filesize using your coding. You coding seem not working on my side and empty size but if i changed the $fullpath to "$path/$dir/$file" it worked out great and filesize were showing up on my page like a charm!

Thanks Rocknbil for showing me your coding advice :)

$fullpath = "$path/$dir/$file";
$fsize = filesize($fullpath);
3:51 pm on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Once again . . . anything posted on this forum (by me, at least) is not copy and paste code unless you explicitly see "working example" or "tested code" in the post. These are all typed on the fly and for assistance only. Glad you worked it out.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month