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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting files

Status
Not open for further replies.

kaptlid

Technical User
Nov 26, 2006
86
US
How do you delete files in a directory. For example I want to delete all files that ARE NOT of the jpg extension.

I used this code but it deleted EVERYTHING!
Code:
$originalfilelist = scandir($memberdir);
foreach($originalfilelist as $files => $values) { chmod($values, 0777 ); if ( (substr($values,-3) !== 'jpg') or (substr($values,-3) !== 'JPG')){ unlink($file.'/'.$values); } }
 
At first glance, it looks like your comparison operators are incorrect. !== is not 'not equal to.' For string comparison, try using the strcmp function:

i.e.
Code:
if ( strcmp( (substr($values,-3), 'jpg' ) )
 
Thanks for the tip on the strcmp function I used this code below to do what I needed it to do.

Code:
$originalfilelist = scandir($memberdir);
foreach($originalfilelist as $files => $values) { chmod($values, 0777 ); 
if (strcasecmp(substr($values,-3), 'jpg') == 0) 
{ $null; } else {  unlink($file.'/'.$values); } }
 
i'm confused by your code. some queries:

1. where does $file get populated? why is it not $memberdir?
2. how do you handle the "." and ".." files?

would this work?

Code:
$files = scandir($memberdir);
foreach ($files as $file){
 $f_data = path_info($memberdir."/".$file);
 if (isfile($memberfile.'/'.$file) && (strtoupper($f_data['extension']) !== "JPG") ){
   unlink ($memberdir."/".$file);
 }
}
 
oh sorry, the $file variable is a directory name (this is part of a much larger script. Its called $file because its part of a zip upload script and I wanted the filename to become the directory name, thats all.)

I am still not sure how to handle the . .. vars, I also get a chmod warning saying the file or directory does not exist. I guess that can just be gotten rid of by putting @ in front of that function.

Thank you for your help.
 
You are right I could have used the $memberdir variable instead of the $file variable, either one is fine in my case.
 
the code i posted handles the . and .. because it performs an is_file() test. this also prevents deletion of sub-directories within the zip architecture, which I notice that your script does not handle at the moment. you might want to make the scandir script recursive.
 
Thank you very much, the script is supposed to delete all subdirectories and any files that are not of the jpg extension within the zip file.
 
then you will need to recurse

something like this perhaps:

Code:
<?php
function deleteFiles($dir){
	
	clearstatcache();	//ensure we are doing a fresh evaluation
	
	$files = scandir($dir);	//read the directory into an array
	
	foreach ($files as $file){
		
		$f_data = path_info($memberdir."/".$file);	//grab the extension
	 	
		if ( isfile($dir.'/'.$file) &&  (strtoupper($f_data['extension']) !== "JPG")){	//test the value
		
			unlink ($dir."/".$file);	//delete the file
	 	
		} elseif (is_dir($dir.'/'.$file) && $file !== "." && $file !=='..'){	// test for a valid directory
		
			deleteFiles($dir.'/'.$file); //recurse
		}
	}		
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top