Php generate directory index

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.

Оцените статью