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

PHP recursively changes all files under a folder

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

Two methods, random and conditional modes
/*
* time: December 2, 2016 17:58:36
* author: Zhang Chenyang
*email:lequne@126.com
*/
//Generate different file names based on judgment conditions
function fRename($dirname)
{
If (!is_dir($dirname)) {
echo "{$dirname} is not a valid directory!";
exit();
}
$handle = opendir($dirname);

While (($fn = readdir($handle)) !== false) {
If ($fn != '.' && $fn != '..') {

echo "fn = " . $fn . "nr";
$curDir = $dirname . '/' . $fn;

echo "curDir = " . $curDir . "rn";
if (is_dir($curDir)) {
fRename($curDir);
} else {

$path = pathinfo($curDir);
//var_dump($path);//die;
//$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];
$img_info = getimagesize($curDir);

$h = $img_info[1];
// var_dump($w);
If ($w == 210 && $h == 210) {
} elseif ($w == 720 && $h == 268) {
} Elseif ($ w == 298 && $ h == 198) {
} elseif ($w == 338 && $h == 365) {
} Elseif ($ w == 425 && $ h == 425) {
‐— }
’ ’ out out. out //$n=$new.'.jpg';
//$newname1='.jpg';
var_dump($curDir);
$newname = str_replace($fn, $newname, $curDir);
var_dump($newname);
echo "newname after replacement:" . $newname . "rn";
. // echo $ curdir .'-- '. $ Newme. "& Lt; br & gt;"; }

}

}
}

//Call this method

//fRename('./001');


//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
function rand_fRename($dirname)
{
Header('content-type:text/html;charset=utf8');
If (!is_dir($dirname)) {
echo "{$dirname} is not a valid directory!";
exit();
}
$handle = opendir($dirname);

While (($fn = readdir($handle)) !== false) {
If ($fn != '.' && $fn != '..') {

echo "fn = " . $fn . "nr";
$curDir = $dirname . '/' . $fn;

echo "curDir = " . $curDir . "rn";
if (is_dir($curDir)) {

// var_dump($new);die;

$newname = str_replace($fn, $new, $curDir);
var_dump($newname);
. rand_fRename($newname);
}
else{

$path = pathinfo($curDir);
//var_dump($path);//die;
//$newname = $path['dirname'].'/'.rand(0,100).'.'.$path['extension'];
//123[1] $img_info=getimagesize($curDir);
// var_dump($img_info);

$w=$img_info[0];
$h=$img_info[1];

// Prevent duplicate random generating file names
$new=rand(10,100000000000);
$n=$new.'.jpg';
//$newname1='.jpg';
var_dump($curDir);
$newname = str_replace($fn,$n,$curDir);
var_dump($newname);
echo "newname after replacement:".$newname."rn";

}

}

}
}

rand_fRename(); //The folder folder that needs to be changed

//Randomly generate string
function generate_password($length = 8)
{
Header('content-type:text/html;charset=utf8');
// Password character set, you can add any characters you need
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$password = "";
for ($i = 0; $i < $length; $i++) {
// There are two ways to obtain characters here
// The first is to use substr to intercept any character in $chars;
// The second is to take any element of the character array $chars
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
}
Return $password;
}


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
    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!