PHP batch file name modification method analysis,

Source: Internet
Author: User

PHP batch file name modification method analysis,

This example describes how to modify file names in batches in PHP. We will share this with you for your reference. The details are as follows:

Here we use an example written by myself in the battlefield to analyze the ideas and precautions for modifying file names in batches using PHP.

In this example, you will see how PHP judges whether a path is a directory, How to Use PHP to obtain the file name and file extension in the path, and the basic application of the random function rand, and the basic application of recursive functions in the core content of this program.

Program functions:Use the PHP Directory and file function to traverse all the files and folders in the given directory and modify the file name;

First, determine whether the user-provided directory is a valid directory;

Here we only modify the narrow file name (excluding Directories), so in the program, we must determine whether it is a directory, if it is a directory, open this directory to traverse all the files in the directory. If not, we should use a random name to modify the original file name (it is not scientific to use a random number as the name, but this is not our focus ).

The specific procedure is as follows:

<? Php // use the PHP Directory and file function to traverse all the files and folders in the directory given by the user, 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. "<br>" ;}}}// a directory name is provided to call the function fRename ('pl');?>

Program Analysis:

In row 3, we use the is_dir function to determine whether the directory given by the user is a valid directory name. Because we are learning, it is easy to write. If you want to put it on the server for your use, you must carefully verify the input data-for example, whether the Directory allows user modification ......

Pay attention to the judgment of Row 3 of the function. You must use the full equals sign (PHP full equal sign = three equal signs, incomplete, etc! = One exclamation point and two equal signs). Otherwise, if one file name is 0 (zero), the program will be terminated unexpectedly;

Row 3: If the file to be read is a directory, call the function itself here to implement recursion of the function;

Note the usage of the 16th-row pathinfo function. This function returns an array with three elements. The three elements are:

The directory name of the Dirname path, the name of the file contained in the basename path, and the extension of the extension file. For the usage of this function, see the relevant sections of the manual.

In line 17 of the program, PHP's random number function rand is used. This is a basic random function. For other random functions, see the manual http://shouce.jb51.net/php5 /.

Note: It is important to determine whether the names of the files read using the PHP readdir function are "." and "...". Why? You can try it!

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.