Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Need assistance on this error..

    Error
    Code:
    Warning:  Invalid argument supplied for foreach() in /home/mysite/public_html/tms/adminpanel/systeminfo.php  on line 150
    Code at line 150

    PHP Code:
        }
    echo 
    "</table>
    "
    ;
    function 
    filesize_r($path)
        {
       if(!
    file_exists($path))
               return 
    0;
       if(
    is_file($path))
               return 
    filesize($path);
       
    $ret 0;
       foreach(
    glob($path."/*") as $fn)
         
    $ret += filesize_r($fn);
       return 
    $ret;
     } 
    Last edited by djdubuque; 03-25-2010 at 06:30 PM. Reason: That was the one, thank you

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That suggests you're not getting the expected result (Array) from glob(). Test the return value before using it in a foreach. According to the documentation, possible return values are "an array containing the matched files/directories, an empty array if no file matched or FALSE on error."
    PHP Code:
    $arr glob($path."/*");
    if (
    is_array($arr) && !empty($arr))
    {
      
    // do foreach and return

    Are you a Help Vampire?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Probably the recurse thats causing that. Fairly sure that glob returns its items without the filepath, so you'll need to recall filesize_r with $path . '/' . $fn as its argument.
    Another option is to look at using the RecursiveDirectoryIterator/RecursiveIteratorIterator classes if available for use.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    That suggests you're not getting the expected result (Array) from glob(). Test the return value before using it in a foreach. According to the documentation, possible return values are "an array containing the matched files/directories, an empty array if no file matched or FALSE on error."
    PHP Code:
    $arr glob($path."/*");
    if (
    is_array($arr) && !empty($arr))
    {
      
    // do foreach and return


    OK, this corrected the error, but the result is "0"

    Site Folder Size 0 Bytes

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $ret += filesize_r($fn);
    Should be:

    Code:
    $ret += filesize($fn);

  • Users who have thanked MattF for this post:

    djdubuque (03-25-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •