Unconfigured Ad Widget
Collapse
Announcement
Collapse
No announcement yet.
Errata
Collapse
X
-
Why does the script hardcode the directory separator? In Windows, it's a backslash, so this code is already not portable. And the file name might need to be encoded (safe) before sending to browser. Try this instead:
define(DS, DIRECTORY_SEPARATOR) ; // ... is / on nix and \ on Windows
define(BR,'<br>') ;
$dirName = DS . 'etc';
$dir = opendir($dirName);
while ( ($file = readdir($dir)) ) {
$tempFile = $dirName . DS . $file ;
$tempFileSafe = htmlentities($tempFile, ENT_QUOTES);
if (is_dir($tempFile)) {
echo "Directory: $tempFileSafe", BR;
} elseif (is_file($tempFile)) {
echo "File: $tempFileSafe", BR;
} elseif (is_link($tempFile)) {
echo "Link: $tempFileSafe", BR;
} else {
echo "Unknown: $tempFileSafe", BR;
}
}
closedir($dir);
ChrisLast edited by RingerSoftware; 07-11-2009, 08:59 PM.
-
Chapter 6
Chris,
You are correct in all of your observations.
However the point of this sample code was to demonstrate the usage of the is_dir, is_file and is_link. I think we made the statement several times throughout the book that the code samples are not to be confused with production code. This being both from a best practices standpoint and an error handling standpoint.
Thanks,
Jeff Olen
Comment
Comment