Dan Dan - 24 days ago 15
PHP Question

Hiding .DS_store from scandir()

I am having some problems with a little script that outputs file from a particular directory into a table but it output the annoying .DS_store file as I am running this app on a mac.

Here is my script to put - I have tried various methods that either stop the script working altogether or has no effect.

<?php

echo '<table>';

$dir = 'archive/';
$files = scandir($dir, 0);
for($i = 2; $i < count($files); $i++)
echo '<tr>
<td>' . $files[$i]. '</td>
<td><a href="' . $files[$i]. '" download><button>Download</button></a>
<a href=""></a></td>
</tr>';

echo '</table>';

?>

Answer

You may want to ignore all files that begin with a '.'

._whatever : These files are created on volumes that don't natively support HFS.

.Trash : Used to store files thrown in the Trash but haven't yet been deleted.

.Spotlight-V100 : Stores metadata indexes and indexing rules for Spotlight.

<?php

echo '<table>';

$dir = 'archive/';
$files = scandir($dir, 0);
for($i = 2; $i < count($files); $i++)
  if (preg_match("/^[^\.].*$/", $files[$i])) {
    echo '<tr>
      <td>' . $files[$i]. '</td>
      <td><a href="' . $files[$i]. '" download><button>Download</button></a>
      <a href=""></a></td>
      </tr>';
  }
echo '</table>';
?>
Comments