0

I am working with Zend FW and deleting images from a form submitted in a view. I successfully delete all images name from the database, each one of them in the Array using their IDs.

What I can't solve is once the names are deleted how do I unlink each file form the folder?

I have an hidden fied as follow:

<input type="hidden" name="Image[]" value="<?php echo $this->escape($r['image']); ?>" />

And I know that In the controller I can call a file name using:

$images = $this->_getParam('image');

This is fine for one image but how do I unlink an Array of files? It is the first time that I come across this problem, please help.

I am trying to do something like this:

foreach ($images as $img) { 
            foreach(("/uploads/thumb}/{$img}") as $file) {

        unlink($file); 
         }
    }

I am probably doing something silly...apologies.

frapet
  • 229
  • 1
  • 4
  • 18

2 Answers2

1

You use the foreach (http://php.net/manual/en/control-structures.foreach.php) control structure to iterate through the array. You use unlink then to every $value inside the loop.

It should look something like this:

foreach ($images as $img) { 
    unlink('uploads/thumb/'.$img);
}
Martin Müller
  • 2,565
  • 21
  • 32
  • Hi Martin, I have tried using it but it does not seem to work – frapet Dec 03 '12 at 15:38
  • There is no error reported. It just does not delete the files in the folder – frapet Dec 03 '12 at 15:42
  • Hi martin I have edited the question. Could you adive what I ma doing wrong? Please. – frapet Dec 03 '12 at 15:52
  • Hi Martin. Thank you for your help. Is there any chance you could have a look at a similar question?: [link] (http://stackoverflow.com/questions/13723589/zend-framework-and-php-unlink-file) – frapet Dec 05 '12 at 13:40
1

Taking in consideration the help from vascowhite I managed to make it work this way. the simplest and abvious really:-

foreach($images as $img) { 
        $file = "./uploads/thumb/$img";
        unlink($file); 
}
vascowhite
  • 18,120
  • 9
  • 61
  • 77
frapet
  • 229
  • 1
  • 4
  • 18