Delete folder contents from PHP

I have a PHP script that needs to delete a series of jpgs in a folder  the folder path is:

/var/www/vhosts/mydomain.com/httpdocs/images

How do I script this to achieve this but leave the folder intact?
pda4meAsked:
Who is Participating?
 
kivan24Commented:
Dellete all jpg files in folder by mask:
<?php
chdir("/var/www/vhosts/mydomain.com/httpdocs/images");
$mask = "*.jpg";
array_map("unlink", glob($mask));
?>

Open in new window


Delete only files listed in array:
<?php
chdir("/var/www/vhosts/mydomain.com/httpdocs/images");
$filelist = array("image1.jpg", "image2.jpgs");
array_map("unlink", $filelist);
?>

Open in new window

0
 
Marco GasiFreelancerCommented:
Try this:

<?php
$dir = '/var/www/vhosts/mydomain.com/httpdocs/images';
$iterator = new DirectoryIterator($dir);
$iterrator->rewind;
while($iterator->valid){
  if ($iterator->isFile() && ! $iterator-> isDot()){
    $ext = explode('.', $iterator->getFilename());
    if ($ext = 'jpg'){
        unlink($iterator->getFilename());
    }
  }
}
?>
0
 
pda4meAuthor Commented:
Worked GREAT!  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.