• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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?
0
pda4me
Asked:
pda4me
1 Solution
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now