Jump to content

Recommended Posts

Hi. New to the site, thanks for letting me become a member.

I have a one page directory listing index.php.

The php excludes filenames and folders that begin with a dot ".".

I also need the code to exclude folders which begin with an underscore "_"

I have some syncronise folders in my website which hold xml files and must reside in the places they are currently. I'm very new to php so if possible if a resolution can be made please provide as much info as you can.

Thanks.



<?php

// Adds pretty filesizes
function pretty_filesize($file) {
    $size = filesize($file);
    if ($size < 1024) {
        $size = $size . " Bytes";
    } elseif (($size < 1048576) && ($size > 1023)) {
        $size = round($size / 1024, 1) . " KB";
    } elseif (($size < 1073741824) && ($size > 1048575)) {
        $size = round($size / 1048576, 1) . " MB";
    } else {
        $size = round($size / 1073741824, 1) . " GB";
    }
    return $size;
}

function cmp($a, $b)
{
    if ($a['isdir'] == $b['isdir'])
        return strcmp($a['filename'], $b['filename']);
    return $a['isdir'] && !$b['isdir'] ? -1 : 1;
}

date_default_timezone_set("Europe/London");

// Checks to see if viewing hidden files is enabled
/*
  if ($_SERVER['QUERY_STRING'] == "hidden") {
    $hide = "";
    $ahref = "./";
    $atext = "Hide";
  } else {

    $hide = ".";
    $ahref = "./?hidden";
    $atext = "Show";
  }
 */

$hide = ".";

if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || substr($_SERVER['QUERY_STRING'],0,2) == ".." || strstr($_SERVER['QUERY_STRING'], "..")) {
    $currdir = ".";
} else {
    $currdir = urldecode($_SERVER['QUERY_STRING']);
}

if ($currdir == ".") 
    $label = "M&M Group Audit Documents";
else {
    $path = explode('/', $currdir);
    $label = $path[count($path)-1]; 
}

?>

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="shortcut icon" href="./.favicon.ico">
        <title><?= $label ?></title>

        <link rel="stylesheet" href="./.style.css">
        <script src="./.sorttable.js"></script>
    </head>

    <body>
        <div id="container">
            <h1><?= $label ?></h1>

            <table class="sortable">
                <thead>
                    <tr>
                        <th>File Name</th>
                        <th>File Type</th>
                        <th>File Size</th>
                        <th>Last Modified</th>
                    </tr>
                </thead>
                <tbody>
<?php

// Opens directory
$myDirectory = opendir($currdir) or die();

// Gets each entry
while (false !== ($entryName = readdir($myDirectory))) {
    $dirArray[] = array('filename' => $entryName, 'isdir' => is_dir($currdir.'/'.$entryName));
}

// Closes directory
closedir($myDirectory);

// Counts elements in array
$indexCount = count($dirArray);

// Sorts files
usort($dirArray, "cmp");

// Loops through the array of files
for ($index = 0; $index < $indexCount; $index++) {

    // Decides if hidden files should be displayed, based on query above.
    if (substr($dirArray[$index]['filename'], 0, 1) != $hide || ($currdir != '.' && $dirArray[$index]['filename'] == "..")) {
    if(in_array($file, $ignore_file_list)) { continue; }

        // Resets Variables
        $favicon = "";
        $class = "file";

        // Gets File Names
        $name = $dirArray[$index]['filename'];
        $namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index]['filename'];
        $fullname = $currdir . '/' . $dirArray[$index]['filename'];

        // Gets Date Modified
        $modtime = date("M j Y g:i A", filemtime($fullname));
        $timekey = date("YmdHis", filemtime($fullname));


        // Separates directories, and performs operations on those directories
        if (is_dir($fullname)) {
            $extn = "<Folder>";
            $size = "<Folder>";
            $sizekey = "0";
            $class = "dir";

            // Gets favicon.ico, and displays it, only if it exists.
            if (file_exists("$namehref/favicon.ico")) {
                $favicon = " style='background-image:url($namehref/favicon.ico);'";
                $extn = "<Website>";
            }

            // Cleans up . and .. directories
            if ($name == ".") {
                $name = ". (Current Directory)";
                $extn = "<System Dir>";
                $favicon = " style='background-image:url($namehref/.favicon.ico);'";
            }
            if ($name == "..") {
                $name = ".. (Previous Folder)";
                $extn = "<System Dir>";
            }
            if ($currdir == "." && $dirArray[$index]['filename'] == "..")
                $namehref = "";
            elseif ($dirArray[$index]['filename'] == "..") {
                $dirs = explode('/', $currdir);
                unset($dirs[count($dirs) - 1]);
                $prevdir = implode('/', $dirs);
                $namehref = '?' . $prevdir;
            }
            else
                $namehref = '?' . $namehref;
        }

        // File-only operations
        else {
            // Gets file extension
            $extn = pathinfo($dirArray[$index]['filename'], PATHINFO_EXTENSION);

            // Prettifies file type
            switch ($extn) {
                case "png": $extn = "PNG Image";
                    break;
                case "jpg": $extn = "JPEG Image";
                    break;
                case "jpeg": $extn = "JPEG Image";
                    break;
                case "svg": $extn = "SVG Image";
                    break;
                case "gif": $extn = "GIF Image";
                    break;
                case "ico": $extn = "Windows Icon";
                    break;

                case "txt": $extn = "Text File";
                    break;
                case "log": $extn = "Log File";
                    break;
                case "htm": $extn = "HTML File";
                    break;
                case "html": $extn = "HTML File";
                    break;
                case "xhtml": $extn = "HTML File";
                    break;
                case "shtml": $extn = "HTML File";
                    break;
                case "php": $extn = "PHP Script";
                    break;
                case "js": $extn = "Javascript File";
                    break;
                case "css": $extn = "Stylesheet";
                    break;

                case "pdf": $extn = "PDF Document";
                    break;
                case "xls": $extn = "Spreadsheet";
                    break;
                case "xlsx": $extn = "Spreadsheet";
                    break;
                case "doc": $extn = "Microsoft Word Document";
                    break;
                case "docx": $extn = "Microsoft Word Document";
                    break;

                case "zip": $extn = "ZIP Archive";
                    break;
                case "htaccess": $extn = "Apache Config File";
                    break;
                case "exe": $extn = "Windows Executable";
                    break;

                default: if ($extn != "") {
                        $extn = strtoupper($extn) . " File";
                    } else {
                        $extn = "Sconosciuto";
                    } break;
            }

            // Gets and cleans up file size
            $size = pretty_filesize($fullname);
            $sizekey = filesize($fullname);
        }

        // Output 
        echo("
        <tr class='$class'>
            <td sorttable_customkey='$index'><a href='$namehref'$favicon class='name'>$name</a></td>
            <td><a href='$namehref'>$extn</a></td>
            <td sorttable_customkey='$sizekey'><a href='$namehref'>$size</a></td>
            <td sorttable_customkey='$timekey'><a href='$namehref'>$modtime</a></td>
        </tr>");
    }
}
?>

                </tbody>
            </table>

        <!-- <h2><?php echo("<a href='$ahref'>$atext hidden files</a>"); ?></h2> -->
        </div>
    </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/287338-exclude-file-pattern/
Share on other sites

Hi. I use Allway Sync to syncronise files from our server to my web site. Allway Sync leaves a folder containing an xml file. The folder which is left is called _SYNCCAP. This folder is seen by users who logon to the website as it is a file list. I just need to hide this _SYNCAPP folder from the user.

The php code hides files, folders starting with a dot, I was hoping the modification of the code could also exclude files, folders starting with an underscore, or the folder _SYNCAPP in particular.

 

Thanks

I've never heard of it before and it's only compatible with windows platform, that's why I don't know it. All files and folders starting with a dot (.) symbol are hidden in the Unix world, windows is not Unix but obviously that works as well. What about to add a dot in front of _SYNCAPP directory to make it hidden? 

Yes I've thought of that. I've asked the software supplier of Allway Sync if this can be done in preferences of the software, they say no..

I don't think a rename function would help. The Allway Sync software looks for the _SYNCAPP folders xml file to see if any changes have occured from the the source documents and upgrade them if so.

The software looks for the folder _SYNCAPP  explicitly so I think it just needs to be hidden.

 

Kind Regards

Edited by DaveWillett

Hm....yes, my idea was to make this folder hidden renaming it inside Allway Sync Software, but obviously it's an impossible task. If you try to rename that folder in your website this could bring into conflict with Allway Sync. What do you think?

Yep, we can't rename it either in its own software or within the php code.

But can we make it invisible. There is a query within the php code to not show files beginning with a dot. Can we add to this code to ignore files beginning with an underscore also?

I do not know how to edit the code..

 

Regards

Why don't you create a hidden directory and let Allway Sync to look for the _SYNCAPP folders there?

 

 

There is a query within the php code to not show files beginning with a dot.

 

What do you mean by saying this? How the members browse their files and directories in yours web site using a control panel or something different?

I have no control over SYNCAPP, the software puts the folder wherever it requires to syncronise files.

 

The query is in the first posting but I have no idea how to edit it to exclude folders starting with an underscore.:

// Checks to see if viewing hidden files is enabled
/*
  if ($_SERVER['QUERY_STRING'] == "hidden") {
    $hide = "";
    $ahref = "./";
    $atext = "Hide";
  } else {

    $hide = ".";
    $ahref = "./?hidden";
    $atext = "Show";
  }
 */

$hide = ".";

if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || substr($_SERVER['QUERY_STRING'],0,2) == ".." || strstr($_SERVER['QUERY_STRING'], "..")) {
    $currdir = ".";
} else {
    $currdir = urldecode($_SERVER['QUERY_STRING']);
}

So, you want to say that you have no control to say where the soft to create this _SYNCAPP directory in your web site?

You can disable all requests using an apache mod_secuity instead of writting a ton of php scripts.

Edited by jazzman1

You're losing me Guru now.

The _SYNCAPP folder must reside in the directory where the local sync software creates it via ftp, I have no control of this.

Simply can the php code be changed to not show files or folders starting with an underscore as it already does with a file or folder starting with a dot?

 

Regards

I don't have any idea how this software works that's why I'm asking stupid questions :) As I stated above all files and directories starting with dot (.) sign are considered as hidden, so, php doesn't (cannot) hide them because they are already hidden, what php does is to disable all requests to them whose requests coming from the browser or other clients software using same http ports and protocols. You could redirect or just give some notice to every request to those hidden directories/files or directories/files starting with underscore using apache rules instead writting a php script to achieve this. Can you tell me how the members browse their files by browser or other software?   

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.