astarmathsandphysics Posted February 10, 2015 Share Posted February 10, 2015 I have a directory lising script at astarmathsandphysics.com, listing maths notes Ithe script I am using displays images that I would like to exclude from the list How do I do this? This is the listing code I am using if($this->files){ $count = 0; foreach ($this->files as $file) { $row_style = ($row ? "one" : "two"); print "<tr class=\"row ".$row_style.(++$count == count($this->files)?" last":"")."\">\n"; print "<td class=\"name\">\n"; print "\t\t<a href=\"".$this->location->getDir(false, true, false, 0).$file->getNameEncoded()."\""; if(EncodeExplorer::getConfig('open_in_new_window') == true) print "target=\"_blank\""; print " class=\"item file"; if($file->isValidForThumb()) print " thumb"; print "\">"; print $file->getNameHtml(); if($this->mobile == true) { print "<span class =\"size\">".$this->formatSize($file->getSize())."</span>"; } print "</a>\n"; print "</td>\n"; if($this->mobile != true) { print "<td class=\"size\">".$this->formatSize($file->getSize())."</td>\n"; print "<td class=\"changed\">".$this->formatModTime($file->getModTime())."</td>\n"; } if($this->mobile == false && GateKeeper::isDeleteAllowed()){ print "<td class=\"del\"> <a data-name=\"".htmlentities($file->getName())."\" href=\"".$this->makeLink(false, false, null, null, $this->location->getDir(false, true, false, 0).$file->getNameEncoded(), $this->location->getDir(false, true, false, 0))."\"> <img src=\"?img=del\" alt=\"Delete\" /> </a> </td>"; } print "</tr>\n"; $row =! $row; }} Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/ Share on other sites More sharing options...
raphael75 Posted February 10, 2015 Share Posted February 10, 2015 (edited) You should be able to do something like this: if($this->files){ $count = 0; foreach ($this->files as $file) { $filename_parts = explode('.', $file); //break up filename into an array $extension = $filename_parts[(count($filename_parts) -1)]; //get the last array element - the extension if(($extension == 'gif') || ($extension == '.png')){ //if the extension is .gif or .png continue; //go to the next file } $row_style = ($row ? "one" : "two"); print "<tr class=\"row ".$row_style.(++$count == count($this->files)?" last":"")."\">\n"; print "<td class=\"name\">\n"; print "\t\t<a href=\"".$this->location->getDir(false, true, false, 0).$file->getNameEncoded()."\""; if(EncodeExplorer::getConfig('open_in_new_window') == true) print "target=\"_blank\""; print " class=\"item file"; if($file->isValidForThumb()) print " thumb"; print "\">"; print $file->getNameHtml(); if($this->mobile == true) { print "<span class =\"size\">".$this->formatSize($file->getSize())."</span>"; } print "</a>\n"; print "</td>\n"; if($this->mobile != true) { print "<td class=\"size\">".$this->formatSize($file->getSize())."</td>\n"; print "<td class=\"changed\">".$this->formatModTime($file->getModTime())."</td>\n"; } if($this->mobile == false && GateKeeper::isDeleteAllowed()){ print "<td class=\"del\"> <a data-name=\"".htmlentities($file->getName())."\" href=\"".$this->makeLink(false, false, null, null, $this->location->getDir(false, true, false, 0).$file->getNameEncoded(), $this->location->getDir(false, true, false, 0))."\"> <img src=\"?img=del\" alt=\"Delete\" /> </a> </td>"; } print "</tr>\n"; $row =! $row; }} Edited February 10, 2015 by raphael75 Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505363 Share on other sites More sharing options...
Ch0cu3r Posted February 10, 2015 Share Posted February 10, 2015 @raphael75 why not use $extension = pathinfo($file, PATHINFO_EXTENSION); to get the file extension? Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505369 Share on other sites More sharing options...
raphael75 Posted February 10, 2015 Share Posted February 10, 2015 That works, too. Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505382 Share on other sites More sharing options...
CroNiX Posted February 10, 2015 Share Posted February 10, 2015 It might be better to check the mime type rather than rely on the file extension, unless you've already done that when the images were uploaded. I can rename dangerous_code.php to lovely_image.jpg pretty easily Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505383 Share on other sites More sharing options...
astarmathsandphysics Posted February 11, 2015 Author Share Posted February 11, 2015 Doesnt work so far Here is an example page. http://astarmathsandphysics.com/notes/?dir=gcse-physics Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505419 Share on other sites More sharing options...
raphael75 Posted February 11, 2015 Share Posted February 11, 2015 Try this: if($this->files){ $count = 0; foreach ($this->files as $file) { $cur_file = $this->location->getDir(false, true, false, 0).$file->getNameEncoded(); $finfo = finfo_open(FILEINFO_MIME_TYPE); $cur_mime_type = finfo_file($finfo, $cur_file); if(($cur_mime_type == 'image/gif') || ($cur_mime_type == 'image/png')){ continue; } $row_style = ($row ? "one" : "two"); print "<tr class=\"row ".$row_style.(++$count == count($this->files)?" last":"")."\">\n"; print "<td class=\"name\">\n"; print "\t\t<a href=\"".$this->location->getDir(false, true, false, 0).$file->getNameEncoded()."\""; if(EncodeExplorer::getConfig('open_in_new_window') == true) print "target=\"_blank\""; print " class=\"item file"; if($file->isValidForThumb()) print " thumb"; print "\">"; print $file->getNameHtml(); if($this->mobile == true) { print "<span class =\"size\">".$this->formatSize($file->getSize())."</span>"; } print "</a>\n"; print "</td>\n"; if($this->mobile != true) { print "<td class=\"size\">".$this->formatSize($file->getSize())."</td>\n"; print "<td class=\"changed\">".$this->formatModTime($file->getModTime())."</td>\n"; } if($this->mobile == false && GateKeeper::isDeleteAllowed()){ print "<td class=\"del\"> <a data-name=\"".htmlentities($file->getName())."\" href=\"".$this->makeLink(false, false, null, null, $this->location->getDir(false, true, false, 0).$file->getNameEncoded(), $this->location->getDir(false, true, false, 0))."\"> <img src=\"?img=del\" alt=\"Delete\" /> </a> </td>"; } print "</tr>\n"; $row =! $row; }} Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505425 Share on other sites More sharing options...
astarmathsandphysics Posted February 11, 2015 Author Share Posted February 11, 2015 Whey they that works. Only 500 or so more problems to solve converting my site to php 1 Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505428 Share on other sites More sharing options...
CroNiX Posted February 11, 2015 Share Posted February 11, 2015 How are you getting the files ($this->files) to begin with? You can use php's glob function to only get files you want from a dir, like: $files = glob('/path/to/files/*.html'); //$files is now an array of all .html files in the /path/to/files/ dir Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505429 Share on other sites More sharing options...
raphael75 Posted February 11, 2015 Share Posted February 11, 2015 Whey they that works. Only 500 or so more problems to solve converting my site to php Have you added it to the example page (http://astarmathsandphysics.com/notes/?dir=gcse-physics) yet? Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505453 Share on other sites More sharing options...
astarmathsandphysics Posted February 12, 2015 Author Share Posted February 12, 2015 yes I added it. It works perfectly. I have written 6000 or so pages of maths and physics notes at the rate of 2 a day. Try to make maintenance easier. Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505512 Share on other sites More sharing options...
astarmathsandphysics Posted February 12, 2015 Author Share Posted February 12, 2015 As for getting files I want all the html files listed. Iwas inserting the links in a menu file manually, but when I have hundreds to do its a real drag. Quote Link to comment https://forums.phpfreaks.com/topic/294498-exlude-gif-png-images-from-directory-listing-script/#findComment-1505513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.