Count number of files in folder in php

后端 未结 6 1233
礼貌的吻别
礼貌的吻别 2020-12-09 10:53


        
相关标签:
6条回答
  • 2020-12-09 11:22

    Glob returns an array, on error it returns false.

    Try this:

    $directory = '/var/www/ajaxform/';
    $files = glob($directory . '*.jpg');
    
    if ( $files !== false )
    {
        $filecount = count( $files );
        echo $filecount;
    }
    else
    {
        echo 0;
    }
    
    0 讨论(0)
  • 2020-12-09 11:23

    There is a mistake in your glob pattern (in the if). You are missing a *:

    glob($directory . '*.jpg')
    

    should work

    0 讨论(0)
  • 2020-12-09 11:27

    Minimalization approach:

    function getImagesNo($path)
    {
      return ($files=glob($path.'*.jpg')) ? count($files) : 0;
    }
    
    0 讨论(0)
  • 2020-12-09 11:28

    Just try this--

    if (glob($directory . "*.jpg") != false)
    $filecount = count(glob($directory . "*.jpg"));
    else
    $filecount = 0;
    
    0 讨论(0)
  • 2020-12-09 11:30

    glob is case sensitive, according to the PHP docs. Are your extensions lowercase? Does the executing account have access to /var/www/ajaxform/?

    0 讨论(0)
  • 2020-12-09 11:37

    Try this:

    <?php 
    $directory = '/var/www/ajaxform/';
    if (glob($directory . '*.jpg') != false)
    {
     $filecount = count(glob($directory . '*.jpg'));
     echo $filecount;
    }
    else
    {
     echo 0;
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题