PHP empty directory without recursive function - php

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/*"));

Related

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

get name of single file in a directory using php

Although I can find plenty of examples of listing all of the files in a directory
$dir = '../upload/'.$id.'/ask_temp';
But I need to get the name of a single file so I can store it in a variable to use elsewhere.
There is and only ever will be one file in there.
In regards to ComFreek's answer, make sure to filter out any directory. (. and ..)
I'm assuming you're using at least PHP 5.3
$files = array_filter(scandir($dir), function($val) use($dir)
{
return is_file($dir.'/'.$val);
});
$myFile = $files[0];
Another way is to this, this is probably easier. (first 2 are always '.' and '..')
$files = scandir($dir);
$myFile = $files[2];

Delete specific filenames in an array using PHP

I have a list of PDFs and I would like to delete them. Can someone please explain to a beginner how this is done? At this point, I can do this in Excel a number of different ways but how can I simply do this in PHP?
Most simple solution:
<?php
$dir = '/files_directory/';
$files_to_delete = array('file1.pdf', 'file3.pdf', 'file4.pdf');
foreach($files_to_delete as $file)
{
$file_path = $dir . $file;
if(is_file($file_path))
{
unlink($file_path);
}
}
Use PHP to search for the folder, do a loop through the files there and add some conditional statements to the loop. If they are true or false, act on those results.
As you have not typed any code for us, I am not going to type any code back to you.
The most simple way I could think of doing it:
array_map('unlink', $array_with_files_to_delete);
$array_with_files_to_keep = array_diff($list_with_pdfs, $array_with_files_to_delete);

Delete images from a folder

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

Searching for specific file extensions in a folder/directory (PHP)

I'm trying to design a program in PHP that would allow me to find files with specific file extensions (example .jpg, .shp etc) in a known directory which consists of multiple folders.
Sample code, documentation or information about what methods I will be required to use will be much appreciated.
glob is pretty easy:
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
There are a few suggestions for recursive descent at the readdir page.
Take a look at PHP's SPL DirectoryIterator.
I believe PHP's glob() function is exactly what you are looking for:
http://php.net/manual/en/function.glob.php
Use readdir to get a list of files, and fnmatch to work out if it matches your required filename pattern. Do all this inside a function, and call your function when you find directories. Ask another question if you get stuck implementing this (or comment if you really have no idea where to start).
glob will get you all the files in a given directory, but not the sub directories. If you need that too, you will need to: 10. get recursive, 20. goto 10.
Here's the pseudo pseudocode:
function getFiles($pattern, $dir) {
$files = glob($dir . $pattern);
$folders = glob($dir, GLOB_ONLYDIR);
foreach ($folders as $folder) {
$files = $files + getFiles($folder);
}
return $files;
}
The above will obviously need to be tweaked to get it working, but hopefully you get the idea (remember not to follow directory links to ".." or "." or you'll be in infinite loop town).

Categories