Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>read the class structure, you will get some useful features</p> </blockquote> <pre><code>&lt;?php // this will call your given callback with the file or folder informations, so you can use your logic to delete/copy/move the file/folder // note: this is a recursive function $literator = new ipDirLiterator( "your/path/to/root/or/anyfolder/", // for getting root path, you can use $_SERVER["DOCUMENT_ROOT"] array( "file" =&gt; function( $file ) { // your callback to delete files if ( basename( $file["pathname"] ) !== basename( __FILE__ ) ) { unlink( $file["pathname"] ); } } ), true ); class ipDirLiterator { protected $basepath = false; protected $callbacks = false; protected $checked = array(); public function __construct( $basepath = false, $callbacks = false, $init = false ) { $this-&gt;basepath = ( $basepath ) ? realpath( $basepath ) : false; $this-&gt;callbacks = $callbacks; if ( $init ) { $this-&gt;literate(); } } public function literate( $dir = false ) { if ( !$this-&gt;basepath ) { return false; } if ( $dir === $this-&gt;basepath ) { return false; } if ( !$dir ) { $dir = $this-&gt;basepath; } $dir = realpath( $dir ); if ( strstr( $dir, basename( $this-&gt;basepath ) ) === false ) { return false; } if ( in_array( $dir, $this-&gt;checked ) ) { return false; } $this-&gt;checked[] = $dir; $items = new DirectoryIterator( $dir ); foreach( $items as $item ) { if ( $item-&gt;isDot() ) { if ( $item-&gt;getFilename() === ".." ) { $this-&gt;literate( dirname( $item-&gt;getPath() ) ); } $this-&gt;callback( "dot", $this-&gt;info( $item ) ); continue; } if ( $item-&gt;isFile() || $item-&gt;isLink() ) { $this-&gt;callback( "file", $this-&gt;info( $item ) ); } if ( $item-&gt;isDir() &amp;&amp; !$item-&gt;isLink() ) { $this-&gt;literate( $item-&gt;getPathname() ); $this-&gt;callback( "dir", $this-&gt;info( $item ) ); } } } private function info( $item ) { $info = array( "filename" =&gt; $item-&gt;getFilename(), "extension" =&gt; pathinfo( $item-&gt;getFilename(), PATHINFO_EXTENSION ), "pathname" =&gt; $item-&gt;getPathname(), "path" =&gt; $item-&gt;getPath(), "readable" =&gt; (bool)$item-&gt;isReadable(), "writable" =&gt; (bool)$item-&gt;isWritable(), "executable" =&gt; (bool)$item-&gt;isExecutable(), "created_on" =&gt; $item-&gt;getCTime(), "last access" =&gt; $item-&gt;getATime(), "modified_on" =&gt; $item-&gt;getMTime(), "inode" =&gt; $item-&gt;getInode(), "permissions" =&gt; $item-&gt;getPerms(), "is_dir" =&gt; (bool)$item-&gt;isDir(), "is_dot" =&gt; (bool)$item-&gt;isDot(), "type" =&gt; $item-&gt;getType(), "group" =&gt; $item-&gt;getGroup(), "owner" =&gt; $item-&gt;getOwner(), "size" =&gt; $item-&gt;getSize() ); return $info; } private function callback( $callback = "file", $args = null ) { if ( $this-&gt;callbacks ) { if ( isset( $this-&gt;callbacks[$callback] ) ) { call_user_func( $this-&gt;callbacks[$callback], $args ); } } } } ?&gt; </code></pre> <p>Detailed example: </p> <pre><code>&lt;?php /** * It will recursively go through the all directories and files under the directory you have given. * Here we are going to delete all the files order than 1 hour **/ /** * Path to the folder you want to process **/ $basepath = "any/path/to/files/or/folder/"; /** * Callbacks **/ $file_callback = function( $file /* information of current file in the loop */ ) { // your callback for files $modified_time = filemtime( $file["pathname"] ); $current_time = time(); $time_differnce = ( $current_time - $modified_time ); if ( $time_differnce &gt; 3600 ) { unlink( $file["pathname"] ); } }; $folder_callback = function( $file /* information of current folder in the loop */ ) { // your callback for folders // proceess your folder here }; /** * Initialize the class **/ $literator = new ipDirLiterator( $basepath, array( "file" =&gt; $file_callback, "dir" =&gt; $folder_callback ) ); $literator-&gt;literate(); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload