Mavent Posted August 17, 2011 Share Posted August 17, 2011 Hello all; I have a script that lists the contents of a Directory. Everything fine so far. It works great, as you can see here: http://www.testnest.com/upload/list.php The script for that page is here: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php // open this directory $myDirectory = opendir("."); // get each entry while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } // close directory closedir($myDirectory); // count elements in array $indexCount = count($dirArray); // Print ("$indexCount files<br>\n"); // sort 'em sort($dirArray); // print 'em print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n"); print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n"); // loop through the array of files and print them all for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>"); print("<td>"); print(filetype($dirArray[$index])); print("</td>"); print("<td>"); print(filesize($dirArray[$index])); print("</td>"); print("</TR>\n"); } } print("</TABLE>\n"); ?> </body> </html> Here's where it goes so horribly wrong. I cannot have the file within the Upload directory. So, I moved the script out, and changed this: $myDirectory = opendir("."); to this: $myDirectory = opendir("upload"); The result can be seen here: http://www.testnest.com/listOut.php As you can see, it's throwing errors now. BTW, here are the offending lines: Line 36: print(filetype($dirArray[$index])); Line 39: print(filesize($dirArray[$index])); Obviously, those two lines aren't referencing the Directory correctly, but I cannot for the life of me figure out why. Any help would be much appreciated. Thanks! Kyle Link to comment https://forums.phpfreaks.com/topic/245044-script-breaking-outside-of-target-directory/ Share on other sites More sharing options...
QuickOldCar Posted August 17, 2011 Share Posted August 17, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php // open this directory $myDirectory = opendir("upload"); // get each entry while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } // close directory closedir($myDirectory); //count elements in array $indexCount = count($dirArray); // Print ("$indexCount files<br>\n"); // sort 'em sort($dirArray); // print 'em print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n"); print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n"); // loop through the array of files and print them all for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print("<TR><TD><a href=\"./upload/$dirArray[$index]\">$dirArray[$index]</a></td>"); print("<td>"); print(filetype("./upload/$dirArray[$index]")); print("</td>"); print("<td>"); print(filesize("./upload/$dirArray[$index]")); print("</td>"); print("</TR>\n"); } } print("</TABLE>\n"); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/245044-script-breaking-outside-of-target-directory/#findComment-1258704 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Try using this. $dir = 'upload'; $myDirectory = opendir($dir); Then use print(filetype($dir .'/'. $dirArray[$index])); Link to comment https://forums.phpfreaks.com/topic/245044-script-breaking-outside-of-target-directory/#findComment-1258710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.