Jump to content

How to recursively delete files with out dealting specific files


Adrienk

Recommended Posts

So I am having issues. like mental issues over this....

 

I found a piece of code on the php manual site in relation to recursively deleting directories and modified it to this:

 

function delete_contents_in_folder($path_to_dir){
if(is_file($path_to_dir)){
	return @unlink($path_to_dir);
}
elseif(is_dir($path_to_dir)){
	$scan = glob(rtrim($path_to_dir,'/').'/*');
	foreach($scan as $index=>$path){
		$this->delete_contents_in_folder($path);
	}
	if($path_to_dir == AISISCORE){
		return;
	}else{
		return @rmdir($path_to_dir);
	}
}
}

 

essentially AISISCORE is the directory i want it to IGNORE. so essentially it deletes every file, sub directory and so on in AISISCORE but does not delete the root directory, in the end AISISCORE is left as an empty folder.

 

What's the issue?

 

I want to tell it to delete everything in AISISCORE as it already does BUT ignore specific files when doing so, that's the part I can't figure out how to do. Your help is appreciated.

Link to comment
Share on other sites

I worked on something similar for an application. My code was first to delete the files in the directory and then delete the directory. But I changed it around so you can see what how you can just delete the files but leave the directory alone:

 

if( file_exists( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] ) ) {
$files = glob( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] . '/*' );
	foreach( $files as $file ) unlink($file);
}

 

I would probably do something like this with your code:

 

function delete_contents_in_folder($path_to_dir){
if(file_exists($path_to_dir) && ($path_to_dir != 'AISISCORE')) {
	$files = glob( $path_to_dir . '/*' );
	foreach( $files as $file ) unlink($file);
	rmdir( $path_to_dir );
} else {
	$files = glob( $path_to_dir . '/*' );
	foreach( $files as $file ) unlink($file);
}
}

 

I haven't tested it and not 100% sure if it will work, but you can test it out on a test directory first to make sure.

Link to comment
Share on other sites

I worked on something similar for an application. My code was first to delete the files in the directory and then delete the directory. But I changed it around so you can see what how you can just delete the files but leave the directory alone:

 

if( file_exists( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] ) ) {
$files = glob( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] . '/*' );
	foreach( $files as $file ) unlink($file);
}

 

I would probably do something like this with your code:

 

function delete_contents_in_folder($path_to_dir){
if(file_exists($path_to_dir) && ($path_to_dir != 'AISISCORE')) {
	$files = glob( $path_to_dir . '/*' );
	foreach( $files as $file ) unlink($file);
	rmdir( $path_to_dir );
} else {
	$files = glob( $path_to_dir . '/*' );
	foreach( $files as $file ) unlink($file);
}
}

 

I haven't tested it and not 100% sure if it will work, but you can test it out on a test directory first to make sure.

 

This, from my understanding - correct me if I am wrong. sais: delete all files except the directory I'm deleting files in. Which is fine. how ever I need to delete a;ll files in said directory EXCEPT for specified files. So essentially: AISISCORE would be empty - EXCEPT for the files I don't want deleted.

 

maybe I missed something in your example?

 

thanks for the help :D

Link to comment
Share on other sites

* create an array of directories you do NOT want to delete

* generate an array of directories in the folder

* iterate through the existing directories

* conditionally delete if not in your safe directory array

 

<?php

$files_i_like = array( 'horses.php', 'dogs.php', 'pr0n.jpg');

//while in your loop
if( !in_array( $file, $files_i_like) ) unlink( $file );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.