Delete images from a folder - php

I want to to destroy all images within a folder with PHP how can I do this?

foreach(glob('/www/images/*.*') as $file)
if(is_file($file))
#unlink($file);
glob() returns a list of file matching a wildcard pattern.
unlink() deletes the given file name (and returns if it was successful or not).
The # before PHP function names forces PHP to suppress function errors.
The wildcard depends on what you want to delete. *.* is for all files, while *.jpg is for jpg files. Note that glob also returns directories, so If you have a directory named images.jpg, it will return it as well, thus causing unlink to fail since it deletes files only.
is_file() ensures you only attempt to delete files.

The easiest (non-recursive) way is using glob():
$files = glob('folder/*.jpg');
foreach($files as $file) {
unlink($file);
}

$images = glob("images/*.jpg");
foreach($images as $image){
#unlink($image);
}

use unlink and glob function
for more see this link
http://php.net/manual/en/function.unlink.php
and
http://php.net/manual/en/function.glob.php

Related

Delete folder and all inside contents function returns "directory not empty"

Below is my attempt to delete a folder and all its content. A folder may contain zip files and folders with files.
public function deleteFolder($dir){
if(file_exists($dir)){
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it,
\RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
}
}
but it returns the following error:
rmdir(C:\Juliver\UIUX\pd-loader\loader/temp/utso-pulgada-pd-loader-5066a7e0298a):
Directory not empty in C:\Juliver\UIUX\pd-loader\loader\Patcher.php on line 95
line 95 points to rmdir($dir); line
If I check the folder utso-pulgada-pd-loader-5066a7e0298a, I see that it's already empty but it throws me the above error.
$dirname = 'C:/Users/Admin/Desktop/test';
array_map('unlink', glob("$dirname/*.*"));
rmdir($dirname);
try this, this remove all the file present in the folder, and that folder too
Directory may contain other directories so you have to use a recursive function.
function removeDir($path) {
$files = glob("$path/*");
foreach ($files as $file) {
if (is_dir($file)) {
removeDir($file);
} else {
unlink($file);
}
}
rmdir($path);
}
Now is enough to call removeDir("/my/nice/path");
If you see the directory already empty, try to check for hidden files and be sure that you have the right permissions.
I suspect you have already checked its not a file permissions issue. As your code works for me but not you, it makes me wonder if it is a to do with PHP file stat or Real Path caching.
Unlinking a file should clear stat cache for individual file automatically. However PHP bugs have previously been known to cause this issue with rmdir.
Try doing a clearstatcache after the rmdir statement in your foreach block.
Previously I've used glob (mentioned in other answers) so I've no idea how RecursiveDirectoryIterator works re file handles; as a long shot try destroying these objects ( unset($files); unset($it) ) before your final rmdir.

About PHP manager file

How about this function?
What does foreach and rmdir($file) do in this code?
function rmDirectory($dir)
{
foreach (glob($dir . '/*') as $file) {
if (is_dir($file))
rmdir($file);
else
unlink($file);
}
rmdir($dir);
}
The foreach construct provides an easy way to iterate over arrays of $dir.
glob — Find pathnames matching a pattern
rmdir — Removes directory
Delete all files and empty directories under $dir
If the $dir is empty, delete it
The glob() function returns an array of filenames or directories
matching a specified pattern.
Source. Your foreach traverses each items found at the pattern specified as parameter. For each of them, is_dir checks whether they are directories. If the current $file happens to be a directory, then it is removed using rmdir, otherwise it is a file and is removed using unlink. Finally $dir is removed as well.
rmDirectory essentially removes the content (folders and files) of a directory and then removes the directory itself.

PHP empty directory without recursive function

Can anyone suggest the best way to empty a folder using PHP. I do not want to use the recursive approach. Is there any alternative and what is that function/approach?
Thanks
Have you tried using a combination of array_map, unlink and glob like this :
array_map('unlink', glob("/path/to/folder/*"));
Credits to Stichoza's answer
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
Use below code
array_map('unlink', glob("path/test/*"));

How can I delete all files in a folder, which matches a certain pattern?

I have a folder with images.
As example:
z_1.jpg
z_2.jpg
z_3.jpg
//...
I want to delete every image with prefix z_*.jpg. How can I do that?
unlink('z_*.jpg'); ?
You need the exact filename to unlink() a file. So just use glob() to get all files which you want to grab. Loop through the returned array and delete the files, e.g.
<?php
$files = glob("z_*.jpg");
foreach($files as $file)
unlink($file);
?>

Check if a file matches a wildcarded spec, in a given directory, with PHP

I have a directory that files are uploaded to, and I want to be able to display a download link if the file exists. The file however has to match a particular pattern as this is the identifier of who uploaded it.
The pattern starts with /ClientFiles/ then it needs to find all files that starts with the user ID. So for example: /ClientFiles/123-UploadData.xls
So it would need to look in the ClientFiles directory and find all files that start with '123-' no matter what comes after.
Cheers
To look for files by a certain pattern you can use glob, then use is_readable to check if you can read the files.
$files = array();
foreach(glob($dirname . DIRECTORY_SEPARATOR . $clientId . '-*' as $file) {
if(is_readable($file) {
$files[] = $file;
}
}
Simply use the file_exists() function
php has a function file_exists. Use that to make some logic about if you show a link or not.

Categories