File older than php

tdebatty / delete_older_than.php

A simple PHP function to delete files older than a given age. Very handy to rotate backup or log files, for example.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

/**
* A simple function that uses mtime to delete files older than a given age (in seconds)
* Very handy to rotate backup or log files, for example.
*
* $dir String whhere the files are
* $max_age Int in seconds
* return String[] the list of deleted files
*/
function delete_older_than ( $ dir , $ max_age )
$ list = array ();
$ limit = time() — $ max_age ;
$ dir = realpath( $ dir );
if (!is_dir( $ dir ))
return ;
>
$ dh = opendir( $ dir );
if ( $ dh === false )
return ;
>
while (( $ file = readdir( $ dh )) !== false )
$ file = $ dir . ‘/’ . $ file ;
if (!is_file( $ file ))
continue ;
>
if (filemtime( $ file ) < $ limit )
$ list [] = $ file ;
unlink( $ file );
>
>
closedir( $ dh );
return $ list ;
>
// An example of how to use:
$ dir = » /my/backups «;
$ to = » my@email.com «;
// Delete backups older than 7 days
$ deleted = delete_older_than( $ dir , 3600 * 24 * 7 );
$ txt = » Deleted » . count( $ deleted ) . » old backup(s): \n» .
implode(«\n», $ deleted );
mail( $ to , » Backups cleanup «, $ txt );

Источник

Delete files older than x days or after x amount of time in PHP

Today we’ll show you how to delete files older than x days or after x amount of time in PHP. Sometimes we need to delete files older than n number of days from a specific directory.

You can also use the following article to delete all files and subfolders from the directory.

In this article, we will show you a simple way to remove files older than specified days or time from the directory using PHP.

Delete files from directory

1. Older than x days

Here, we will create a function where we will pass two parameters, first one is directory/folder path and second parameter is number of days.

This script deletes files from specified folder only, not from child folders.

2. Older than x amount of time

Now, we will write a script to delete files that are x amount of time old.

3. Delete files and subfolders older than x amount of time

If you want to delete files and subfolders based on the given time then you can use the following code. (Thanks, Amir for sharing your knowledge with us.)

That’s it for today.
Thank you for reading. Happy Coding. 🙂

You may also like.

Insert an array into a MySQL database using PHP - Clue Mediator

Insert an array into a MySQL database using PHP

Add or remove input fields dynamically in PHP using JQuery - Clue Mediator

Add or remove input fields dynamically in PHP using jQuery

Copy a file from one directory to another in PHP - Clue Mediator

Copy a file from one directory to another in PHP

Drag and drop multiple file upload using jQuery, Ajax, and PHP - Clue Mediator

Drag and drop multiple file upload using jQuery, Ajax, and PHP

Partially hide email address in PHP - Clue Mediator

Partially hide email address in PHP

Consume a REST API in PHP - Clue Mediator

Consume a REST API in PHP

Leave a Reply Cancel reply

Search your query

Recent Posts

  • Connect to a MySQL Database Using the MySQL Command: A Comprehensive Guide July 16, 2023
  • Connecting to SSH using a PEM File July 15, 2023
  • How to Add the Body to the Mailto Link July 14, 2023
  • How to Add a Subject Line to the Email Link July 13, 2023
  • How to Create Mail and Phone Links in HTML July 12, 2023

Tags

Join us

Top Posts

Explore the article

We are not simply proficient at writing blog post, we’re excellent at explaining the way of learning which response to developers.

For any inquiries, contact us at [email protected] .

  • We provide the best solution to your problem.
  • We give you an example of each article.
  • Provide an example source code for you to download.
  • We offer live demos where you can play with them.
  • Quick answers to your questions via email or comment.

Clue Mediator © 2023. All Rights Reserved.

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

PHP — how to test / check if directory / folder contains old file / file older than?

What I would like to know is whether there is something in PHP that could check/test, in some simple way, whether a folder/directory contains file older than [sometime], or even how many of such files are there without any kind of «manual» go-through?,Now I am not looking for a solution where you scan or open directory and read its contents file by file. For those looking for such inreliable loop, checkout these functions: scandir(. ) to load all at once and then loop it, and opendir(. ), readdir(. ), stat(. )[‘mtime’] to do it file by file and possibly setting some limit on how many is done and make it at least a bit safer. Obviously there is no built-in function to do that, as this is something too specific. You will have to do it yourself one way or another, just like those shell functions you like so much are doing internally.,Write a function that does that so you can use it where its needed. And naturally PHP offers a bunch of functions that make the whole thing smaller and potentially faster. You can for instance do this:

Write a function that does that so you can use it where its needed. And naturally PHP offers a bunch of functions that make the whole thing smaller and potentially faster. You can for instance do this:

function folder_has_older_file($folder, $time) < return count(array_filter(array_map('filemtime', glob("$folder/*")), function ($a) use ($time) < return $a < $time; >)) > 0; > 

Answer by Johnny Woodward

Thanks for contributing an answer to Unix & Linux Stack Exchange!,Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.,The first will delete files 4 hours old or older using the built-in delete function of find; and the second will delete files 30 days old or older using an rm within an -exec clause., How does the -exec rm -f <> + version handle files with spaces in the name? Could an attacker craft a filename that would cause this to delete something you didn’t want deleted? – pileofrogs Jun 25 ’20 at 16:07

-exec rm -f <> \; (or, equivalently, -exec rm -f <> ‘;’ )
This will run rm -f on each file; e.g.,

rm -f /var/dtpdev/tmp/A1/B1; rm -f /var/dtpdev/tmp/A1/B2; rm -f /var/dtpdev/tmp/A1/B3; … 

Answer by Khloe Ponce

Here, we will create a function where we will pass two parameters, first one is directory/folder path and second parameter is number of days.,Today we’ll show you how to delete files older than x days or after x amount of time in PHP. Sometimes we need to delete files older than n number of days from a specific directory.,Now, we will write a script to delete files that are x amount of time old.,In this article, we will show you a simple way to remove files older than specified days or time from the directory using PHP.

Today we’ll show you how to delete files older than x days or after x amount of time in PHP. Sometimes we need to delete files older than n number of days from a specific directory.

Answer by Marianna Velez

Well here we go, the PHP script that deletes the files that is X number of days old. First script, then a bit of explanation.,if the file entry is not a directory then it fetches the file modified time (last modified time) and compares, if it is number of days old,Now paste this code and save it as a php file, upload it to the folder from where you want to delete the files. You can see at the beginning of this php code,that sets the number of days, for example if you set it to 2 then files older than 2 days will be deleted. Basically this is what happens when you run the script, gets the current directory and reads the file entries, skips ‘.’ for current directory and further checks if there are any other directories,

 if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) < $nofiles++; unlink($dir.'/'.$file); >> closedir($handle); echo "Total files deleted: $nofiles \n"; > ?>

Now paste this code and save it as a php file, upload it to the folder from where you want to delete the files. You can see at the beginning of this php code

that sets the number of days, for example if you set it to 2 then files older than 2 days will be deleted. Basically this is what happens when you run the script, gets the current directory and reads the file entries, skips ‘.’ for current directory and further checks if there are any other directories,

if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) )

if the file entry is not a directory then it fetches the file modified time (last modified time) and compares, if it is number of days old

if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) < $nofiles++; unlink($dir.'/'.$file); >

Answer by Yara Carpenter

To look for a specific file, run the following command from the root (/). The command contains the exact name for the file you are searching for.,To find the files in different directories, add their paths in the command. In our case, we will check in the test and numeric directories.,In our case, we will look for all those starting with the letters file in the test directory.,You can also look for the files in the /home directory modified within the last 10 days using the command;

The general syntax for the find command is

To look for a specific file, run the following command from the root (/). The command contains the exact name for the file you are searching for.

./test/file22.txt ./sales/file22.txt

You can also search for the file in another directory while still in the current location. In this case, you need to provide the path for the directory where you want to search.

In our case, we will look for all those starting with the letters file in the test directory.

./test/file22.txt ./test/file30.doc ./test/file1.txt ./test/file5,doc

To find a file in Linux with a certain extension, add it to the command.

./test/subtest/subfil.txt ./test/file22.txt ./test/file1.txt ./home1/files32.txt ./home2/file22.txt ./qa/tags.txt

It returns the following output

./test/qatree.pdf ./test/qa.txt ./home/qa

Add the type d option to locate directories only.

All searches with -name switch are case sensitive and will not give results with capital letters. To get all cases, use the -iname option.

./test/qatree.pdf ./test/qa.txt ./test/QAtest.txt ./home/qa

To find the files in different directories, add their paths in the command. In our case, we will check in the test and numeric directories.

find ./test ./numeric -name file22.txt -type f
./test/file22.txt /root/numeric/file22.txt
./test/subtest/subfil.txt ./test/qatree.pdf ./test/file22.txt ./test/qa.txt ./test/file30.doc ./books/acro.pdf ./data1/FILE22.txt ./docs/files32.txt

To look for all the files containing the word hyperconvergence”, use;

find / -type f -exec grep -l -i "hyperconvergence" <> ;

To look for the files in a specific directory, simply add them to the command

find ./numeric -type f -exec grep -l -i "hyperconvergence" <> ;

To Search find all 30MB files

Find files larger than a specified size

. /Downloads/ubuntu18043.iso ./.cache/pip/http/6/e/3/8/3/6e38383f41850c3e5705d48116bc52f81b92a80500f414305057 7a9c

Find files less than 10MB in the current directory

When looking for files within a specific range such as between 100 and 200 MB

find / -size +100M -size -200M

Directories

Find files older than n days

find /path/ -type f -name '*.txt' -mtime +8

This will look for files modified within the last 17 hours

Looks for directories modified within the last 10 days

To see files that have not been accessed within the last 10 days in the home directory.

Files accessed exactly 10 days ago

Accessed within the last 10 days

You can also look for the files in the /home directory modified within the last 10 days using the command;

For example, all files modified between 6 and 15 days ago in the home directory.

find /home -type f -mtime +6 -mtime -15

To find the files accessed within the last 10 minutes, use the -amin option.

./.bash_history ./[email protected]:~#

Directories accessed within the last 10 minutes

Find files with permission 777

Источник

Читайте также:  Java workspace in use
Оцените статью