Home > Backend Development > PHP Tutorial > PHP simple traversal rename

PHP simple traversal rename

巴扎黑
Release: 2016-11-23 15:56:56
Original
1337 people have browsed it

$path = './fzlhead/';

function get_filetree_scandir($path){

$result = array();

$temp = array();

if (!is_dir ($path)||!is_readable($path)) return null; //Check directory validity

$allfiles = scandir($path); //Get all files and folders in the directory

foreach ($allfiles as $filename) { //Traverse the files and folders in the directory

if (in_array($filename,array('.','..'))) continue; //Ignore. and..

$fullname = $path.'/'.$filename; //Get the full file path

if (is_dir($fullname)) { //If it is a directory, continue the recursion

$result[$filename] = get_filetree_scandir($fullname); //Recursion starts

}

else {

$temp[] = $filename; //If it is a file, store it in the array

$uniqid = uniqid('apoo_');

$ext = pathinfo( $filename, PATHINFO_EXTENSION);

rename($fullname,$path.'/'.$uniqid.'.'.$ext);

}

}

foreach ($temp as $tmp) { // Store the contents of the temporary array into the array that saves the results

$result[] = $tmp; //This allows the folder to be arranged in the front and the file in the back

}

return $result;

}

print_r( get_filetree_scandir($path));

?>


Related labels:
php
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template