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.
Asked
Active
Viewed 2,085 times
0
-
1unlink is the correct function. I'm not sure what your problem is? – Corbin Jun 06 '12 at 05:55
-
For search filename use opendir http://php.net/manual/en/function.opendir.php – Sergey Jun 06 '12 at 06: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. – Delicja Jun 06 '12 at 06:10
2 Answers
2
Solved.
I used:
$files = glob($path_to_gallery . '/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
unlink($file);
break;
}

Delicja
- 193
- 1
- 8
- 18
1
$path = 'full_path/gallery/';
$dir = opendir($path);
while ($dir && ($file = readdir($dir)) !== false) {
unlink($path.$file);
break;
}

Dezigo
- 3,220
- 3
- 31
- 39
-
Thanks. I don't know what I'm doing wrong. I got (dots): .. not name of jpg file. I use glob. – Delicja Jun 06 '12 at 08:41