Tuesday 15 September 2015

php - deleting all files in except the one running the delete code -


I want to delete all the files in the public_html directory of my domain by a single command. The answer for this question of SO was:

    

How can I add an exception to it, maybe using this code or something else? I would not want to delete the file because it will delete the file, because this file will also be in the public_fiflot folder.

Read the class structure, you will find some useful features

  & lt ;? Php // This file will call your callback from the file or folder, so that you can use your logic to delete / copy / move the file / folder / note: This is a recursive function $ literator = new ipDirLiterator ( You get $ _SERVER ["DOCUMENT_ROOT"] array ("file" => function ($ file) to get your / path / in / root / or / any folder / "// root name" If you can use / delete your callback files (original name ($ Hail [ "Pthanav"])! = = Besanam (__FILE__)) {unlink ($ file [ "pathname"]);}}), true); Class ipDirLiterator {protected $ basepath = false; Protected $ callback = wrong; Protected $ check = array (); Public function __ composition ($ basepath = false, $ callbacks = false, $ init = false) {$ this- & gt; BasePath = ($ basepath)? Real path ($ basepath): Wrong; $ This- & gt; Callback = $ callback; If ($ init) {$ this-> Literate (); }} Public Function Literate ($ dir = false) {If (! $ This-> Basepath) {Return False; } If ($ dir === $ this-> Baspath) {return false; } If (! $ Dir) {$ dir = $ this- & gt; Base path; } $ Dir = realpath ($ dir); If (strstr ($ dir, basename ($ this-> base path)) === false) {return false; } If (in_array ($ dir, $ this- & gt; check)) {return false; } $ This- & gt; Czech [] = $ dir; $ Items = new directory iterator ($ dir); Foreign exchange ($ item as item $) {if ($ item- & gt; hasot ()) {if ($ item- & gt; getFilename () === "..") {$ this-> ; Literate (dirname ($ item-> getPath ())); } $ This- & gt; Callback ("dot", $ this-> information ($ item)); to continue; } If ($ item- & gt; isFile ()) $ item- & gt; IsLink ()) {$ this- & gt; Callback ("file", $ this-> information ($ item)); } If ($ item-> Headie) & amp; Amp;! Item- & gt; IsLink ()) {$ this- & gt; Literate ($ item-> getPathname ()); $ This- & gt; Callback ("dir", $ this-> information ($ item)); }}} Private function information ($ item) {$ info = array ("filename" => $ item-> getFilename (), "extension" => gt; pathinfo ($ item- & gt; getFilename ( Getpath (), "pathname" = & gt; $ item- & gt; getPathname (), "path" = & gt; $ item- & gt; getPath (), "readable" = & gt; (bool) $ Item- & gt; heerdable (), "writable =" "= = (bool) $ item-> virtual ()," executable "= & gt; (item) & gt; Getitime (), "made_one" => $ item-> getCTime () "last access" => $ item-> getATime (), "modified_on" => $ item- & GetMeime (), "inode" = & gt; $ item- & gt; getInode (), "permission" = & gt; item- & gt; getPerms (), "is_dir" = & gt; (bool) $ Item- & gt; isDir (), "is_dot" = & gt; (bool) $ item- & gt; T (), "type" = & gt; get-type (), "group" => = item-> getGroup (), "owner" => gt; $ item- & gt; ; GetOwner (), "size" => $ item -> getSize (); return $ info;} {Function ($ $ - callback [$ callback])) {call_user_func ($ this-> ; & Gt; Callback [$ callback], $ elgs); }}}}? & Gt;   

Detailed example:

  & lt ;? Php / ** * It will be recycled through all the directories and files under the directory given to you * Here we are going to order all files over 1 hour ** / / ** * The folder where you have to process The path to it ** / $ basepath = "any / path / to / files / or / folder /"; / ** * Callback ** / $ file_callback = function ($ file / * Current file information in * loop * /) {// your callback for files $ modified_time = filemate ($ file ["pathname"]); $ Current_time = time (); $ Time_differnce = ($ current_time - $ modified_time); If ($ time_differnce> 3600) {unlink ($ file ["pathname"]); }}; $ Folder_callback = Function ($ file / * Current folder information in loop * /) {// Your callbacks / folders for folders here}}; / ** * Start the classroom ** / $ literator = new ipDirLiterator ($ basepath, array ("file" => gt; dollar_callback, "dir" => $ folder_callback)); $ Literator- & gt; Literate (); ? & Gt;    

No comments:

Post a Comment