• Ignoring files/paths
    2 replies, posted
I am currently working on a downloads page for my website, and I'm having a little trouble.. The current PHP code I'm using to load all the possible downloads is here: [code] <?php //define the path as relative $path = "./downloads"; //using the opendir function $dir_handle = @opendir($path) or die("<b> An error occured while opening $path !</b>"); //running the while loop while ($file = readdir($dir_handle)) { if ($file != "../") // <-- My petty attempt to make it ignore some files. echo "<img src='../images/icons/drive-download.png' border='0' /><b><a href='http://bellminator.com/downloads/$file'>$file</a></b><br/>"; } //closing the directory closedir($dir_handle); ?> [/code] It works well enough, but for some reason it also includes both the index site and the downloads folder as a link. I was wondering if theres any way to tell it what not to load, such as if ($file == code) or something along those lines. I'm pretty new to PHP so.. yeah. :frown: (Heres my [url="http://bellminator.com/downloads"]website[/url] if you need it.) [editline]04:37PM[/editline] Ugh, just noticed this wasn't in the web programming section. Mods feel free to move it.
[php] <?php //define the path as relative $path = "./downloads/"; //using the opendir function $dir_handle = @opendir($path) or die("<b> An error occured while opening $path !</b>"); //running the while loop $arrIgnore = array(".", "..", "index.php", "index.html"); while ($file = readdir($dir_handle)) { if (array_search($file, $arrIgnore) === false) { echo "<img src='../images/icons/drive-download.png' style='border: 0px;' /><a href='http://bellminator.com/downloads/" . $file . "' style='font-weight: bold;'>" . $file . "</a><br />"; } } //closing the directory closedir($dir_handle); ?> [/php] Just add shit to the $arrIgnore array if more needs to be ignored. Untested, but should work fine.
[QUOTE=h2ooooooo;21615625][php] <?php //define the path as relative $path = "./downloads/"; //using the opendir function $dir_handle = @opendir($path) or die("<b> An error occured while opening $path !</b>"); //running the while loop $arrIgnore = array(".", "..", "index.php", "index.html"); while ($file = readdir($dir_handle)) { if (array_search($file, $arrIgnore) === false) { echo "<img src='../images/icons/drive-download.png' style='border: 0px;' /><a href='http://bellminator.com/downloads/" . $file . "' style='font-weight: bold;'>" . $file . "</a><br />"; } } //closing the directory closedir($dir_handle); ?> [/php] Just add shit to the $arrIgnore array if more needs to be ignored. Untested, but should work fine.[/QUOTE] Thanks! As far as I can tell it works perfectly.
Sorry, you need to Log In to post a reply to this thread.