steveh62 Posted December 12, 2008 Share Posted December 12, 2008 I have a folder with jpegs in and 1 html file. how with php can I empty the contents (the jpegs or jpg files) and NOT the html file Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/ Share on other sites More sharing options...
rhodesa Posted December 12, 2008 Share Posted December 12, 2008 $dir = 'folder/'; if(!($dir = realpath($dir))) die("Folder not found"); foreach(scandir($dir) as $file){ if($file == '.' || $file == '..') continue; $ext = substr($file,0,strrpos($file,'.')+1); if($ext == 'jpeg' || $ext == 'jpg'){ unlink($dir.'/'.$file); print "Removed $dir/$file<br>"; } } Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/#findComment-713668 Share on other sites More sharing options...
steveh62 Posted December 12, 2008 Author Share Posted December 12, 2008 that was swift...thanks! Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/#findComment-713679 Share on other sites More sharing options...
Yesideez Posted December 12, 2008 Share Posted December 12, 2008 rhodesa - just curious - could you use implode() for the $ext assignment then just check the last element? Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/#findComment-713711 Share on other sites More sharing options...
Mark Baker Posted December 12, 2008 Share Posted December 12, 2008 rhodesa - just curious - could you use implode() for the $ext assignment then just check the last element? Yup, you could also use pathinfo ($file,PATHINFO_EXTENSION) Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/#findComment-713717 Share on other sites More sharing options...
rhodesa Posted December 12, 2008 Share Posted December 12, 2008 rhodesa - just curious - could you use implode() for the $ext assignment then just check the last element? it would be explode, but yes... Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/#findComment-713722 Share on other sites More sharing options...
Yesideez Posted December 12, 2008 Share Posted December 12, 2008 explode() - yes, you're right - bad typo on my part and is what I was thinking when I typed it! Link to comment https://forums.phpfreaks.com/topic/136678-solved-how-to-empty-folder-contents/#findComment-713778 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.