Enclose these statements ...
asort($dir_display);
foreach($dir_display as $value){
echo "<PRE>";
echo $value;
echo "\n";
echo "</PRE>";
"<br>";
}
in a conditional i.e. ...
if($dir_display){
asort($dir_display);
foreach($dir_display as $value){
echo "<PRE>";
echo $value;
echo "\n";
echo "</PRE>";
"<br>";
}
} else {
echo "No files to view.";
}
If no values have been added to the $dir_display array then it will return false.
P.S. you don't need to do the $x = 0 ... $x++ thing. Just use ...
$dir_display[] = $t.substr($space,0,40-strlen($file));
... and PHP will add this entry to the end of the array.