Jump to content

Exlude .gif .png images from directory listing script


astarmathsandphysics

Recommended Posts

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;
    }
}
 

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;
    }
}

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 :)

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;
    }
}

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
  On 2/11/2015 at 3:14 PM, astarmathsandphysics said:

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.