I have a simple PHP script that delete files from one foler and it looks like this:
$files = glob('all/*');
foreach($files as $file){
if(is_file($file))
unlink($file);
I have two other folders "added" and "old" and I want to delete files in these folders too, how can I do this?
I tried this
$files = glob('all/*,added/*,old/*');
and this
$files = glob('all/*','added/*','old/*');
but it's not working.
$arr = array('all/*','added/*','old/*');
foreach ($arr as $a) {
$files = glob($a);
foreach($files as $file){
if(is_file($file))
unlink($file);
}
}
also a more direct approach can be found here PHP Regex specify multiple paths using glob()
try this:
$folders = [ 'all/*', 'added/*', 'old/*' ];
foreach($folders as $folder) {
$files = glob($folder);
foreach($files as $file){
if(is_file($file)) {
unlink($file);
}
}
}
Related
How to write all the txt files contents in a folder to an html page, besides i don't know the files names, i tried this but it didn't work:
<?php
$files=scandir("D:\\new\places");
foreach (files as value)
echo (files);
?>
you can use following php code to get content of the files.
$path='../innovation';
$files=scandir($path);
//print_r($files);
foreach ($files as $key => $value) {
if($value!="." && $value!="..")
{
print_r(file_get_contents($path."/".$value));
}
}
readfile is ideal for this, since it streams the files directly to the output buffer, which will avoid memory issues for large files.
<?php
$path = 'myDirectory';
$files = glob("{$path}/*.txt");
foreach ($files as $file)
{
readfile($file);
}
If you want to list all the text files (.txt) from a directory to HTML using PHP you can use this code to do it:
<?php
echo '<ul>';
// set the path to the directory...
$dirname = "directory/";
$files = glob($dirname."*.txt");
foreach($files as $file) {
echo '<li>'.$file.'</li>';
}
echo '</ul>';
?>
You can try this:
$directory = "./foldername";
$files = glob($directory . "*.txt");
foreach($files as $file){
$file = fopen($directory . '/' . $file,'r');
while ($line = fgets($file)) {
echo($line);
}
fclose($fh);
}
Use glob() to get all *.txt files in a directory. After loop through every file with a foreach loop and get the content of each file using file_get_contents() and put the content into the target file with file_put_contents()
<?php
$files = glob("path/*.txt");
$output = "output.txt";
foreach($files as $file) {
$content = file_get_contents($file);
print_r($content);
}
?>
I am trying to simply empty all the files in a directory, but I keep getting an error that the path in file_put_contents is a directory.
//empty the cache
$files = scandir('tmp/whazzup/cache/');
if($files!=false){
foreach($files as $file){
file_put_contents('tmp/whazzup/cache/'.$file, '');
}
}
PHP scandir returns an array of all files and directories. So you are getting an array with elements like . and .. and any other sub-directories.
What you should probably do in your foreach loop:
foreach ($files as $file) {
// ignore directories
if (is_dir($file)) {
continue;
}
// process files
file_put_contents(...);
}
I am trying for the life of me to find the best way to delete all files in a single directory excluding a single file extension, ie anything that is not .zip
The current method I have used so far which successfully deletes all files is:
$files = glob('./output/*');
foreach($files as $file)
{
if(is_file($file))
unlink($file); // delete file
}
I have tried modifying this like so:
$files = glob('./output/**.{!zip}', GLOB_BRACE);
foreach($files as $file)
{
if(is_file($file))
unlink($file); // delete file
}
However, I am not hitting the desired result. I have changed the line as follows which has deleted only the zip file itself (so I can do the opposite of desired).
$files = glob('./output/*.{zip}', GLOB_BRACE);
I understand that there are other methods to read directory contents and use strpos/preg_match etc to delete accordingly. I have also seen many other methods, but these seem to be quite long winded or intended for recursive directory loops.
I am certainly not married to glob(), I would simply like to know the simplest/most efficient way to delete all files in a single directory that are not a .zip file.
Any help/advice is appreciated.
$exclude = array("zip");
$files = glob("output/*");
foreach($files as $file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if(!in_array($extension, $exclude)) unlink($file);
}
This code works by having an array of excluded extensions, it loads up all files in a directory then checks for the extension of each file. If the extension is in the exclusion list then it doesn't get deleted. Else, it does.
This should work for you:
(I just use array_diff() to get all files which are different to *.zip and then i go through these files and unlink them)
<?php
$files = array_diff(glob("*.*"), glob("*.zip"));
foreach($files as $file) {
if(is_file($file))
unlink($file); // delete file
}
?>
How about calling to the shell? So in Linux:
$path = '/path/to/dir/';
$shell_command = escapeshellcmd('find ' . $path .' ! -name "*.zip" -exec rm -r {}');
$output = shell_exec($shell_command);
I would simply like to know the simplest/most efficient way to delete all files in a single directory that are not a .zip file.
SPL Iterators are very effective and efficient.
This is what I would use:
$folder = __DIR__;
$it = new FilesystemIterator($folder, FilesystemIterator::SKIP_DOTS);
foreach ($it as $file) {
if ($file->getExtension() !== 'zip') {
unlink($file->getFilename());
}
}
Have you tried this:
$path = "dir/";
$dir = dir($path);
while ($file = $dir->read()) {
if ($file != "." && $file != ".." && substr($file, -4) !== '.zip') {
unlink($file);
}
}
I'm using the following to delete all files from the specified directory.
$files = glob('path/to/temp/*');
foreach($files as $file){
if(is_file($file))
unlink($file);
}
It removes everything other than partially uploaded files eg : myfile.mp3.part
I've tried specifying .part in the file path just to see if I can force it that way :
$files = glob('path/to/temp/*.part');
But that doesn't work either.
Am I missing something here? Is there a different method for deleting non-active partial files?
$files = scandir('/path/to/temp');
foreach($files as $key => $file) {
if ( preg_match('/.*?\.part$/', $file) ) {
unlink($file);
}
}
I'm using something likes this to delete all files in a folder.
$dir = "/path/to/temp";
$files = scandir($dir);
foreach($files as $file){
$path = $dir."/".$file;
if(is_file($path)) unlink($path);
}
Following this thread (first post) I have successfully accomplished the task of deleting all files from a folder using php.
This is the code I use:
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
I would like to exclude some files from being deleted. What code adjustment should I apply?
$files = glob('path/to/temp/*'); // get all file names
$exceptions = ["awesomefile_a", "awesomefile_b"];
foreach($files as $file){ // iterate files
if(is_file($file) && !in_array(end(explode("/", $file)), $exceptions))
unlink($file); // delete file
}