www.webdeveloper.com
Results 1 to 4 of 4

Thread: Hide File

  1. #1
    Join Date
    Mar 2014
    Posts
    1

    Hide File

    Hi. I'm using a php index script for a file listing directory where I only want to show reports.
    The script filters any filename that starts with a dot "." which is great, however, there is one file (css) that must be in the same folder as the reports but cannot start with a dot. I need to hide this specific file but not disable it. Can any of you guys look through the code and advise any changes? I am a complete novice where coding is concerned so would apprteciate as much information as possible.
    Kind Regards.

    Dave

    Code:
    <?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/Rome");
    
    // 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 = "Root";
    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 = "&lt;Cartella&gt;";
                $size = "&lt;Cartella&gt;";
                $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 = "&lt;Website&gt;";
                }
    
                // Cleans up . and .. directories
                if ($name == ".") {
                    $name = ". (Current Directory)";
                    $extn = "&lt;System Dir&gt;";
                    $favicon = " style='background-image:url($namehref/.favicon.ico);'";
                }
                if ($name == "..") {
                    $name = ".. (Previous Folder)";
                    $extn = "&lt;System Dir&gt;";
                }
                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>

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    If you want to hide just a particular CSS file and you know its name, you can either create an if statement that checks each filename so it's never inserted into $dirArray. On the other hand, if you want it to be in the array but just not displayed on your webpage, create an if statement just before creating each table row so that file isn't displayed.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,313
    You could do something with regular expressions, e.g. (untested):
    PHP Code:
    // if (substr($dirArray[$index]['filename'], 0, 1) != $hide || 
    //    ($currdir != '.' && $dirArray[$index]['filename'] == "..")) {

    if(!preg_match('/(^'.preg_quote($hide).'|\.css$)/i'$dirArray[$index]['filename'] || 
        (
    $currdir != '.' && $dirArray[$index]['filename'] == "..")) { 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2014
    Posts
    40
    You can make an array containing all files you want to hide. Then, make a conditionnal statement in the while loop and check if the file/folder is not in the array.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles