I try to make a gallery. In a folder I have some duplicate pictures. I have pictures named: af_160112, af_160113, af_160114. I would like remove this first one. How to take the first picture in a folder and delete it? So far I have known that I should use unlinke($file) function. Thank you for your help.
3
-
1unlink is the correct function. I'm not sure what your problem is?– CorbinCommented Jun 6, 2012 at 5:55
-
For search filename use opendir php.net/manual/en/function.opendir.php– SergeyCommented Jun 6, 2012 at 6:01
-
Sorry, I am not precise. I try to use foreach loop and dir function but I do something wrong. I will edit my first post.– DelicjaCommented Jun 6, 2012 at 6:10
Add a comment
|
2 Answers
Solved.
I used:
$files = glob($path_to_gallery . '/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
unlink($file);
break;
}
$path = 'full_path/gallery/';
$dir = opendir($path);
while ($dir && ($file = readdir($dir)) !== false) {
unlink($path.$file);
break;
}
-
Thanks. I don't know what I'm doing wrong. I got (dots): .. not name of jpg file. I use glob.– DelicjaCommented Jun 6, 2012 at 8:41