Home > php教程 > php手册 > body text

PHP recursively changes all files under a folder

WBOY
Release: 2016-12-05 13:27:49
Original
1360 people have browsed it

Two methods, random and conditional modes
<?php<br> /*<br> * time: December 2, 2016 17:58:36<br> * author: Zhang Chenyang<br> *email:lequne@126.com<br> */<br> //Generate different file names based on judgment conditions<br> function fRename($dirname)<br> {<br> If (!is_dir($dirname)) {<br> echo "{$dirname} is not a valid directory!";<br> exit();<br> }<br> $handle = opendir($dirname);<br> <br> While (($fn = readdir($handle)) !== false) {<br> If ($fn != '.' && $fn != '..') {<br> <br> echo "fn = " . $fn . "nr";<br> $curDir = $dirname . '/' . $fn;<br> <br> echo "curDir = " . $curDir . "rn";<br>                  if (is_dir($curDir)) {<br>                                        fRename($curDir);<br>                     } else {<br> <br>                   $path = pathinfo($curDir);<br>                         //var_dump($path);//die;<br>                       //$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];<br>                                                                                                                                                                                                                                                                                         $img_info = getimagesize($curDir);<br> <br> ​​​​​​​​                    $h = $img_info[1];<br>                                    // var_dump($w);<br> If ($w == 210 && $h == 210) {<br>                                                                                                                                                                                    } elseif ($w == 720 && $h == 268) {<br>                                                                                                                                                                 } Elseif ($ w == 298 && $ h == 198) {<br>                                                                                                                                                                                               } elseif ($w == 338 && $h == 365) {<br>                                                                                                                                                     } Elseif ($ w == 425 && $ h == 425) {<br>                                                                                                                                                                                                                                                                                                                                                                            ‐—                                                                                                                         }<br>                                                                                                                                 ’   ’           out out. out                                 //$n=$new.'.jpg';<br>                              //$newname1='.jpg';<br>                 var_dump($curDir);<br>                        $newname = str_replace($fn, $newname, $curDir);<br>                   var_dump($newname);<br>                                                                                                               echo "newname after replacement:" . $newname . "rn";<br>                                                                                                                                                                                      . // echo $ curdir .'-- '. $ Newme. "& Lt; br & gt;";              }<br> <br>         }<br> <br> }<br> }<br> <br> //Call this method<br> <br> //fRename('./001');<br> <br> <br> //Use PHP directory and file functions to traverse all files and folders in the directory given by the user, and modify the file names randomly<br> function rand_fRename($dirname)<br> {<br> Header('content-type:text/html;charset=utf8');<br> If (!is_dir($dirname)) {<br> echo "{$dirname} is not a valid directory!";<br> exit();<br> }<br> $handle = opendir($dirname);<br> <br> While (($fn = readdir($handle)) !== false) {<br> If ($fn != '.' && $fn != '..') {<br> <br> echo "fn = " . $fn . "nr";<br> $curDir = $dirname . '/' . $fn;<br> <br> echo "curDir = " . $curDir . "rn";<br>                  if (is_dir($curDir)) {<br> ​​​​​​​​ <br>                                                // var_dump($new);die;<br> <br>                      $newname = str_replace($fn, $new, $curDir);<br>                   var_dump($newname);<br>                                                                                                                                                                                      .                                                         rand_fRename($newname);<br>              }<br>        else{<br> <br>                     $path = pathinfo($curDir);<br>                           //var_dump($path);//die;<br>                         //$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];<br>                                                                                                                                                                                     //123[1]                                                                                                                                                                                                                                                      $img_info=getimagesize($curDir);<br>                                                  // var_dump($img_info);<br> <br>                        $w=$img_info[0];<br>                      $h=$img_info[1];<br> <br> // Prevent duplicate random generating file names <br>                      $new=rand(10,100000000000);<br>                                                        $n=$new.'.jpg';<br>                                //$newname1='.jpg';<br>                   var_dump($curDir);<br>                            $newname = str_replace($fn,$n,$curDir);<br>                   var_dump($newname);<br>                                                                                                               echo "newname after replacement:".$newname."rn";<br>                                                                                                                                                                          <br>                }<br> <br>          }<br> <br> }<br> }<br> <br> rand_fRename(); //The folder folder that needs to be changed<br> <br> //Randomly generate string<br> function generate_password($length = 8)<br> {<br> Header('content-type:text/html;charset=utf8');<br> // Password character set, you can add any characters you need <br> $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';<br> $password = "";<br> for ($i = 0; $i < $length; $i++) {<br> // There are two ways to obtain characters here <br> // The first is to use substr to intercept any character in $chars; <br> // The second is to take any element of the character array $chars <br> // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); <br>          $password .= $chars[mt_rand(0, strlen($chars) - 1)];<br> }<br> Return $password;<br> } <br> <br><br>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!