Php batch file name modification project_php tutorial

Source: Internet
Author: User
Php batch modify file name programs. Provides two php code that uses traversal to modify file names in batches. if you need it, refer to it. Copy the following code for an instance? Php uses the PHP directory and file function to provide two php code that uses traversal to modify file names in batches. if you need it, refer to it.

Instance 1

The code is as follows:
// Use the PHP directory and file function to traverse all the files and folders in the user's given directory and modify the file name
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! = '..'){
$ CurDir = $ dirname. '/'. $ fn;
If (is_dir ($ curDir )){
FRename ($ curDir );
}
Else {
$ Path = pathinfo ($ curDir );
$ Newname = $ path ['dirname']. '/'. rand (0,100). '.'. $ path ['extension'];
Rename ($ curDir, $ newname );
Echo $ curDir. '---'. $ newname ."
";
}
}
}
}
// Give a directory name to call the function
FRename ('Pl ');
?>

Instance 2

The code is as follows:

$ Dir = './';

If ($ dh = opendir ($ dir )){
While ($ file = readdir ($ dh ))! = False ){
If ($ file = "." | $ file = "..") continue;
If (filetype ($ dir. $ file) = 'file ')
{
$ Newfile = str_replace ('[1]', '', $ file );
Rename ($ dir. $ file, $ dir. $ newfile );
}
}
Closedir ($ dh );
}

Bytes. The sample code is as follows? Php // use the PHP directory and file function again...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.