Jump to content

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

Edited by raphael75

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.