Empty files in directory - php

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(...);
}

Related

php script to delete file names not matching a dynamic text file

I have created a text file (images.txt) located in /home/users/images.txt, the File contain names of jpeg files. for example:
1.jpeg
12.jpeg
33.jpeg
This file is updated regularly and new image filenames are added
I am looking for a php script that can help in reading the filenames from the .txt and deleting any files from /home/user/images/ directory that does not match the filenames in the .txt file
I have tried the below code and cant get it to work
$array = explode("\n", file_get_contents('/home/user/images.txt'));
$directory = "/home/user/images/";
$files = glob($directory . "*.*");
foreach($files as $file)
{
if (!in_array($file, $array)) {
unlink($directory . $file);
}
}
The names returned by glob() include the directory prefix, but the names in $array don't have them. You need to remove the prefix before searching, and you don't need to add it when calling unlink().
$array = file('/home/user/images.txt', FILE_IGNORE_NEW_LINES);
$directory = "/home/user/images/";
$files = glob($directory . "*.*");
foreach($files as $file)
{
if (!in_array(basename($file), $array)) {
unlink($file);
}
}

How can i move all image files from a directory to a sub-directory using php

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

How to get image names from all sub directories?

I have used following php code to get all images names from relevant directory.
$dir = "images";
$images = scandir($dir);
$listImages=array();
foreach($images as $image){
$listImages=$image;
echo ($listImages) ."<br>";
}
This one works perfectly. But I want to get all images file names within all sub directories from relevant directory. Parent directory does not contain images and all the images contain in sub folders as folows.
Parent Dir
Sub Dir
image1
image2
Sub Dir2
image3
image4
How I go through the all sub folders and get the images names?
Try following. To get full directory path, merge with parent directory.
$path = 'images'; // '.' for current
foreach (new DirectoryIterator($path) as $file) {
if ($file->isDot()) continue;
if ($file->isDir()) {
$dir = $path.'/'. $file->getFilename();
$images = scandir($dir);
$listImages=array();
foreach($images as $image){
$listImages=$image;
echo ($listImages) ."<br>";
}
}
}
Utilizing recursion, you can make this quite easily. This function will indefinitely go through your directories until each directory is done searching. THIS WAS NOT TESTED.
$images = [];
function getImages(&$images, $directory) {
$files = scandir($directory); // you should get rid of . and .. too
foreach($files as $file) {
if(is_dir($file) {
getImages($images, $directory . '/' . $file);
} else {
array_push($images, $file); /// you may also check if it is indeed an image
}
}
}
getImages($images, 'images');

Deleting ".part" files from folder with PHP

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

deleting all files from a folder, excluding some files from being deleted

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
}

Categories