Link to home
Start Free TrialLog in
Avatar of Yong Scott
Yong ScottFlag for Malaysia

asked on

Why i list my directory file got dots

<?php
include 'config.php';

if(isset($_POST['btn-upload']))
{    

$log_directory = '/XAMPP/htdocs/mydoc';

$results_array = array();

if (is_dir($log_directory))
{
        if ($handle = opendir($log_directory))
        {
                //Notice the parentheses I added:
                while(($file = readdir($handle)) !== FALSE)
                {
                        $results_array[] = $file;
                }
                closedir($handle);
        }
}

//Output findings
foreach($results_array as $value)
{
    echo $value . '<br />';
}
}
?>

Open in new window


User generated image
Anyone know how to throw the dots .. because i only want to display my file
Avatar of Dr. Klahn
Dr. Klahn

A simple way to remove them from your output is to evaluate the length of the filename inside the "foreach", and echo only if the length is three or more.
or make a condition to exclude them in the loop:

                while(($file = readdir($handle)) !== FALSE)
                {
                      if (!($file == ".") && !($file == "..")) {
                        $results_array[] = $file;
                      }
                }

Open in new window

Couple of suggestions:

1/ If you are interested only in your files, which i presume have .csv extension, you are better of getting files of .csv extension only.

2/ If date is part of your filename, I strongly suggest that you use yyyyMMdd format for naming files. Highly helpful while sorting and identifying the file in list of files. Or at least think of making day part two digits 02 instead of just 2. That itself will improve the view. Same for month, if not already done.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, if you want a quick shortcut for getting all your CSV files, just use glob():
<?php
include 'config.php';

if(isset($_POST['btn-upload']))
{    
  $log_directory = '/XAMPP/htdocs/mydoc';
  $results_array = array();

  if (is_dir($log_directory))
  {
    $results_array = glob($log_directory . "/*.csv");
  }

  //Output findings
  foreach($results_array as $value)
  {
    echo $value . '<br />';
  }
}
?>

Open in new window

That code will make $results_array contain ONLY the CSV files inside that folder and will automatically skip over the . and .. entries.
Avatar of Yong Scott

ASKER

Good Explain man .. thank you