Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flow control issue with rename()

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi :)

As you can see, I'm trying to add a directory renaming feature to the function below thanks to the insertion of this line :

rename($path . "/", str_replace("/temp/", "/new/", $path . "/"));

The problem is that, very logically, once a directory is renamed, the renaming prevents the function from continuing its scan based on an old directory name. Well, that's the way I understand it ;)

What I would like to know is how to force the function to start over once a directory has been renamed.

Thanks a lot for the help :)

Code:
function hd_dir_scan($directory, $name_before = FALSE, $name_after = FALSE, $filter = FALSE) {
    
    // if the path has a slash at the end we remove it here
    if(substr($directory,-1) == "/") $directory = substr($directory,0,-1);

    // if the path is not valid or is not a directory ...
    if(!file_exists($directory) || !is_dir($directory)) {
    
  	// ... we return false and exit the function
  	return FALSE;
    
    // ... else if the path is readable
    } else if(is_readable($directory)) {
    
  	// we open the directory
  	$directory_list = opendir($directory);
    
      	// and scan through the items inside
      	while (FALSE !== ($file = readdir($directory_list))) {
      	
        		// if the filepointer is not the current directory
        		// or the parent directory
        		if($file != "." && $file != "..") {
        		
        			// we build the new path to scan
        			$path = $directory . "/" . $file;
        
          			// if the path is readable
          			if(is_readable($path)) {
          			
          				// we split the new path by directories
          				$subdirectories = explode("/",$path);
          
            				// if the new path is a directory
            				if(is_dir($path)) {

            				rename($path . "/", str_replace("/temp/", "/new/", $path . "/"));

          					// add the directory details to the file list
          					$directory_tree[] = array(
          						'path'    => $path,
          						'name'    => end($subdirectories),
          						'kind'    => 'directory',
          
          						// we scan the new path by calling this function
          						'content' => hd_dir_scan($path, $filter));
            
            				// if the new path is a file
            				} else if(is_file($path)) {
            				
            				// get the file extension by taking everything after the last dot
            				$extension = end(explode(".",end($subdirectories)));
            
              					// if there is no filter set or the filter is set and matches
              					if($filter === FALSE || $filter == $extension) {
              					
              					// add the file details to the file list
              					$directory_tree[] = array(
              						'path'      => $path,
              						'name'      => end($subdirectories),
              						'extension' => $extension,
              						'size'      => filesize($path),
              						'kind'      => 'file');
              							
              					}
            					
            				}
          				
          			}
        			
        		}
      		
      	}
    	
  	// close the directory
  	closedir($directory_list); 
  
  	// return file list
  	return $directory_tree;
    
    // if the path is not readable ...
    } else {

    return FALSE;	
    
    }
  
}
 

Problem solved !

I didn't know that return could be used to run a function :)

Thus, I just did :

rename($path . "/", str_replace("/temp/", "/new/", $path . "/"));
return hd_dir_scan( ....

Which automatically stops the function currently running and then restarts it again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top