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);
}
Related
I am attempting to move all images from my /webfiles directory to my /webfiles/images directory. I have managed to do it to a single image using the below code:
$imgfiles = glob("webfiles/28.png");
rename($imgfiles[0], "webfiles/images/28.png");
However i have multiple images and the names will be unknown so cannot specify as per the above.
// Get array of all source files
$files = scandir("source");
// Identify directories
$source = "webfiles/";
$destination = "webfiles/images/";
// Cycle through all source files
foreach ($files as $file) {
if (in_array($file, array(".",".."))) continue;
// If we copied this successfully, mark it for deletion
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
// Delete all successfully-copied files
foreach($delete as $file) {
unlink($file);
}
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 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);
}
}
}
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
}