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);
}
}
Related
What I'm trying to do is, to delete files which includes only such type of extensions.
Let's say a folder Images/ contains following files,
a.jpg
b.jpeg
c.php
d.php2
c.png
So now I want to delete only those c.php,d.php2. Is there any way to do this in a single step or any ideas ?
Note: In this case, I do not know exact name of the file or extension.
Thank you in advance.
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>
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 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);
}
}
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
}
i have searched through the Internet and found the scrip to do this but am having some problems to read the file names.
here is the code
$dir = "folder/*";
foreach(glob($dir) as $file)
{
echo $file.'</br>';
}
this display in this format
folder/s0101.htm
folder/s0692.htm
for some reasons i want to get them in this form.
s0101.htm
s0692.htm
can anyone tell me how to do this?
Just use basename() wrapped around the $file variable.
<?php
$dir = "folder/*";
foreach(glob($dir) as $file)
{
if(!is_dir($file)) { echo basename($file)."\n";}
}
The above code ignores the directories and only gets you the filenames.
You can use pathinfo function to get file name from dir path
$dir = "folder/*";
foreach(glob($dir) as $file) {
$pathinfo = pathinfo($file);
echo $pathinfo['filename']; // as well as other data in array print_r($pathinfo);
}