- glubo / gist:2358361
- polarblau / directory_index.class.php
- Majkl578 / gist:2304479
- Saved searches
- Use saved searches to filter your results more quickly
- License
- Chansig/DirectoryIndex
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- nristock/phIndex
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
glubo / gist:2358361
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/usr/local/bin/php |
/** |
* @author Michael Moravec |
*/ |
$ targetDir = $ argc > 1 ? $ argv [ 1 ] : getcwd(); |
if (!is_dir( $ targetDir )) throw new \ InvalidArgumentException (» Invalid dir ‘ $ targetDir ‘. «); |
if (!is_writable( $ targetDir )) throw new \ InvalidArgumentException (» Directory ‘ $ targetDir ‘ is not writable. «); |
$ h = fopen( $ targetDir . DIRECTORY_SEPARATOR . ‘index.html’ , ‘w’ ); |
fwrite( $ h , » ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
$ files = iterator_to_array( new \ CallbackFilterIterator ( new \ FilesystemIterator ( $ targetDir ), function ( $ item ) |
return $ item -> isFile () && $ item -> getFilename () !== ‘index.html’ ; |
>)); |
usort( $ files , function ( $ a , $ b ) |
return strcmp( $ a -> getFilename (), $ b -> getFilename ()); |
>); |
$ template = ‘%s’ . PHP_EOL . ‘ ‘ . PHP_EOL ; |
foreach ( $ files as $ file ) |
$ href = htmlspecialchars(rawurlencode( $ file -> getFilename ())) |
$ label = htmlspecialchars( $ file -> getFilename (); |
$ line = sprintf( $ template , $ href , $ label ); |
fwrite( $ h , $ line ); |
> |
fwrite( $ h , ‘ ‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘ ‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘Generated on: ‘ ); |
fwrite( $ h , ( new \ DateTime ())-> format ( ‘d.m.Y H:i:s’ )); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fclose( $ h ); |
polarblau / directory_index.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class DirectoryIndex |
private $ root ; |
private $ items = array (); |
private $ exclude = array ( ‘.DS_Store’ , ‘.’ , ‘..’ , ‘index.php’ ); |
function __construct ( $ root ) |
$ this -> root = $ root ; |
$ this -> read_contents_in_path (); |
> |
public function get_path () |
$ path = null ; |
if (isset( $ _GET [ ‘path’ ]) && !empty( $ _GET [ ‘path’ ])) |
$ path = $ _GET [ ‘path’ ]; |
if (! $ this -> is_in_scope ( $ _GET [ ‘path’ ])) |
$ path = null ; |
> else |
$ path = ‘/’ . $ path ; |
> |
> |
return $ path ; |
> |
public function get_parent_url () |
$ path = $ this -> get_path (); |
$ parent_path = null ; |
if ( $ path ) |
$ parent_path = ‘?path=’ . substr(dirname( $ this -> root . $ path ), strlen( $ this -> root ) + 1 ); |
> |
return $ parent_path ; |
> |
private function is_in_scope ( $ path , $ recursive = true ) |
$ directory = realpath( $ this -> root ); |
$ parent = realpath( $ path ); |
while ( $ parent ) |
if ( $ directory == $ parent ) return true ; |
if ( $ parent == dirname( $ parent ) || ! $ recursive ) break ; |
$ parent = dirname( $ parent ); |
> |
return false ; |
> |
public function get_items () |
$ items = $ this -> items ; |
uasort( $ items , function ( $ a , $ b ) |
$ a_is_folder = $ a -> is_folder (); |
$ b_is_folder = $ b -> is_folder (); |
if ( $ a_is_folder === $ b_is_folder ) |
return strcmp( $ a -> get_human_name (), $ b -> get_human_name ()); |
> |
return intval( $ b_is_folder ) — intval( $ a_is_folder ); |
>); |
return $ items ; |
> |
private function read_contents_in_path () |
$ path = $ this -> get_path (); |
foreach (glob( $ this -> root . $ path . ‘/*’ ) as $ file ) |
$ file = realpath( $ file ); |
if (!in_array(basename( $ file ), $ this -> exclude )) |
array_push( $ this -> items , new DirectoryItem ( $ file , $ this -> root )); |
> |
> |
> |
> |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class DirectoryItem |
private $ file ; |
function __construct ( $ file , $ root ) |
$ this -> file = $ file ; |
$ this -> root = $ root ; |
> |
public function get_link_url () |
if (is_file( $ this -> file )) |
$ link = substr( $ this -> file , strlen( $ this -> root ) + 1 ); |
return $ link ; |
> else |
$ link = substr( $ this -> file , strlen( $ this -> root ) + 1 ); |
return ‘?path=’ . urlencode( $ link ); |
> |
> |
public function get_human_name () |
$ info = pathinfo( $ file ); |
return ucfirst(str_replace( ‘_’ , ‘ ‘ , basename( $ this -> file , ‘.’ . $ info [ ‘extension’ ]))); |
> |
public function get_file_type () |
if ( $ this -> is_folder ()) |
$ type = ‘folder’ ; |
> else |
$ extension = pathinfo( $ this -> file , PATHINFO_EXTENSION ); |
if (in_array( $ extension , array ( ‘jpg’ , ‘jpeg’ , ‘gif’ , ‘png’ ))) |
$ type = ‘image’ ; |
> elseif ( $ extension == ‘pdf’ ) |
$ type = ‘pdf’ ; |
> else |
$ type = ‘file’ ; |
> |
> |
return $ type ; |
> |
public function get_last_modified_at () |
return strftime( ‘%d.%m.%G %T’ , filemtime( $ this -> file )); |
> |
public function is_image () |
return !!getimagesize( $ this -> path ); |
> |
public function is_folder () |
return is_dir( $ this -> file ); |
> |
> |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Majkl578 / gist:2304479
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/usr/local/bin/php |
/** |
* @author Michael Moravec |
*/ |
$ targetDir = $ argc > 1 ? $ argv [ 1 ] : getcwd(); |
if (!is_dir( $ targetDir )) throw new \ InvalidArgumentException (» Invalid dir ‘ $ targetDir ‘. «); |
if (!is_writable( $ targetDir )) throw new \ InvalidArgumentException (» Directory ‘ $ targetDir ‘ is not writable. «); |
$ h = fopen( $ targetDir . DIRECTORY_SEPARATOR . ‘index.html’ , ‘w’ ); |
fwrite( $ h , » ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
$ files = iterator_to_array( new \ CallbackFilterIterator ( new \ FilesystemIterator ( $ targetDir ), function ( $ item ) |
return $ item -> isFile () && $ item -> getFilename () !== ‘index.html’ ; |
>)); |
usort( $ files , function ( $ a , $ b ) |
return strcmp( $ a -> getFilename (), $ b -> getFilename ()); |
>); |
foreach ( $ files as $ file ) |
fwrite( $ h , ‘ |
fwrite( $ h , htmlspecialchars(rawurlencode( $ file -> getFilename ()))); |
fwrite( $ h , ‘»>’ ); |
fwrite( $ h , htmlspecialchars( $ file -> getFilename ())); |
fwrite( $ h , » ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘ ‘ ); |
fwrite( $ h , PHP_EOL ); |
> |
fwrite( $ h , ‘ ‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘ ‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘Generated on: ‘ ); |
fwrite( $ h , ( new \ DateTime ())-> format ( ‘d.m.Y H:i:s’ )); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fwrite( $ h , PHP_EOL ); |
fwrite( $ h , ‘‘ ); |
fclose( $ h ); |
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Chansig / DirectoryIndex Public archive
Directory Index — The simplest PHP directory Index
License
Chansig/DirectoryIndex
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Directory Index — The simplest PHP directory Index
Directory Index is a simple PHP script that lists the contents of any web-accessable directory and allows navigating there within. Simply upload index.php file to any directory and get immediate access to all files and sub-direcories under that directory. Directory Index is written in PHP 5.3+ and distributed under the MIT License.
- Extremely simple installation
- Creates on-the-fly listing of any web-accessable directory
- Custimizable sort order of files/folders
- Easily define hidden files or file type to be excluded from the listing
Directory Index requires PHP 5.3+ to work properly. For more information on PHP, please visit http://www.php.net.
- Upload src/index.php to the folder you want listed. That’s all!
- For more settings:
- copy src/index.php or execute src/generate.php with the destination in argument and rename index.php.dist in index.php. ex:
php src/generate.php /var/www/mysite - Edit the new index.php and change values in Setting class
- Upload index.php to the folder you want listed.
Ensure you have the latest version of Directory Index installed.
Verify that you have PHP 5.3 or later installed. You can verify your PHP version by running:
Find a problem or bug with Directory Index? Open an issue on GitHub.
Directory Index is distributed under the terms of the MIT License.
About
Directory Index — The simplest PHP directory Index
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
nristock / phIndex Public archive
Self-replicating PHP script to generate dynamic directory indexes.
nristock/phIndex
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Simply copy one of the index.php files into the directory you want to index. Change excludes and formatting as needed. The script will take care of replicating itself in the sub-directory tree.
The visual style of the index generated by index-simple.php apes nginx behaviour. The other one index-fancy.php outputs a visually more appealing index. — Feel free to change the Stylesheets, though 😉
If you ever want to update the script, simply update the one that’s at the root of your index hierarchy. The script will compare timestamps of index.php files in sub-directories and replace them if needed.
While nginx is the web server of my choice its HttpAutoindexModule neither supports an option to exclude files nor does it sort numerical names correctly.
10 would be placed after 1 but before 2 — this script resolves this issue.About
Self-replicating PHP script to generate dynamic directory indexes.
- copy src/index.php or execute src/generate.php with the destination in argument and rename index.php.dist in index.php. ex: