Jump to content

Displaying files of another directory, in a different page.


Tammy

Recommended Posts

<?php
$dirname = '/home/path/glitters/quotes'; // adjust as needed, such as 'C:\web_root\download_folder' for Windows
$webdirname = '/glitters/quotes'; // what the directory appears as from the web browser's point of view

$dir = opendir($dirname);
$file_list = '';


// list files to ignore
$ignore = array( '.', '..', 'index.php');

while(($file = readdir($dir)) != false)
{
    // check that the current file is not within the $ignore list
    // and that it is not a directory
    if(!in_array($file, $ignore) && !is_dir($file))
    {
        // add the file to the list

        $str = substr($file, 0, -4);  /// strip the .php
        $str = explode("-", $str); // explode to remove hyphen(s)
        $str = implode(" ", $str);  // implode the hyphen(s) back into spaces
        $str = explode("_", $str); // explode to remove hyphen(s)
        $str = implode(" ", $str);  // implode the hyphen(s) back into spaces

        $file_list .= "<a class=\"subnav\" href=\"$webdirname/$file\">$str</a>";
    }
}

// and cross your fingers lol

closedir($dir);
?>


<?=$file_list?>

 

Now the code works say if i upload it

folder name glitters

open it via web address mysite.com/cool/glitters

 

it'll show up fine but say,

I php include the path of glitters to the cool index.php page

 

So when you open up the page yoursite.com/cool/

it'll show the files containing in glitters. Without showing the folders, or underscores, in glitters. Because that is what it does.

 

Thanks  :D

Link to comment
Share on other sites

How exactly doesn't it work? Try this:

 

<?php
$path = '/home/path/glitters/quotes';
$webpath = '/glitters/quotes/';

foreach (new DirectoryIterator($path) as $item) {
if (!$item->isFile() || ($filename = $item->getFilename()) == 'index.php' ) {
	continue;
}

echo '<a href="' . $webpath . $filename . '" class="subnav">' . str_replace(array('_', '-'), ' ', basename($filename, '.php')) . '</a><br>';
}
?>

Link to comment
Share on other sites

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.