homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Can get filesize! help
chrissim

5+ Year Member



 
Msg#: 4432029 posted 11:19 am on Mar 22, 2012 (gmt 0)

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>";

}



 

rocknbil

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



 
Msg#: 4432029 posted 3:46 pm on Mar 22, 2012 (gmt 0)

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.

chrissim

5+ Year Member



 
Msg#: 4432029 posted 1:13 am on Mar 23, 2012 (gmt 0)

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

chrissim

5+ Year Member



 
Msg#: 4432029 posted 1:53 am on Mar 23, 2012 (gmt 0)

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)

chrissim

5+ Year Member



 
Msg#: 4432029 posted 8:23 am on Mar 23, 2012 (gmt 0)

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

chrissim

5+ Year Member



 
Msg#: 4432029 posted 2:05 pm on Mar 23, 2012 (gmt 0)

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);

rocknbil

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



 
Msg#: 4432029 posted 3:51 pm on Mar 23, 2012 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved