frapet
frapet

Reputation: 229

Zend framework passing hidden field and unlink file

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.

Upvotes: 0

Views: 1076

Answers (2)

frapet
frapet

Reputation: 229

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); 
}

Upvotes: 1

Martin M&#252;ller
Martin M&#252;ller

Reputation: 2535

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);
}

Upvotes: 1

Related Questions