PHP implementation of bulk modified file name method

Source: Internet
Author: User
This article mainly introduced the PHP batch modification file name method, combined with the example form analysis PHP for file and directory recursion, traversal, modification of the principle and implementation skills, the need for friends can refer to the following

The example in this article describes how PHP bulk modifies file names. Share to everyone for your reference, as follows:

Here we use a field to write an example of the specific analysis of the use of PHP batch modification of file name ideas and considerations.

From this example you will seehow PHP Determines whether a path is a directory, how to get the file name and file extension in the path with PHP, the basic application of the random function rand, and the basic application of the recursive function of the core content of this program .

program function: use PHP directory and file functions to traverse the user to give the directory of all the files and folders, modify the file name;

First, the user to determine whether the directory is a legitimate directory;

We just modify the narrow file name (not including the directory), so in the program must determine whether it is a directory, if it is a directory we open this directory--so that the traversal of all the files in the directory. If it were not for us to use a random name to modify the original file name (with a random number to do the name is not scientific, but this is not our focus).

The specific procedures are as follows:

<?php//Use PHP directories and file functions to traverse all files and folders that the user gives the directory, modify the file name function Frename ($dirname) {if (!is_dir ($dirname)) {echo "{$dirname} 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> ";  } }}}//gives a directory name called function frename (' pl '); >

Program Analysis:

Line 4th We use the Is_dir function to determine whether the user-given directory is not a valid directory name; Because we are learning so it is easier to write if you want to put it on the server for everyone to use, you must carefully verify the input data-for example, whether this directory allows users to modify and so on ...

Note that the function of the 9th line of judgment, must be judged by congruent (php congruent symbol = = = Three equals, not congruent!) = = an exclamation mark two equals sign), otherwise if there is a file name of 0 (0), it will cause the program to terminate unexpectedly;

Line 12th, if the read file is a directory, in this call the function itself, the implementation of recursive function;

Note the use of the PathInfo function in line 16th, which returns an array of three elements with three elements, respectively:

DirName the directory name of the path, the basename path contains the file name and extension of the extension file, please refer to the relevant section of the manual for the use of this function.

In the 17 lines of the program, the PHP random number function Rand is used, which is a basic random function, and other random functions can be viewed in the manual http://shouce.jb51.net/php5/of this site.

Note: Be sure to determine if the file name read with PHP's Readdir function is "." and ".." It's important, why? You can try it!

The above is the whole content of this article, I hope that everyone's study has helped.


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.