php


Delete some files in folder


I have a cronjob that removes every day all unused files, but I will wish to go further. My files are this structure name_number.jpg, but some files have this structure name_.jpg.
Currently my script does not make a difference and removes all. I wish the script to remove the name_number.jpg without erasing the files without number.
$days = 1;
$path = './result/';
// Open the directory
if ($handle = opendir($path))
{
// Loop through the directory
while (false !== ($file = readdir($handle)))
{
// Check the file we're doing is actually a file
if (is_file($path.$file))
{
// Check if the file is older than X days old
if (filemtime($path.$file) < ( time() - ( $days * 24 * 60 * 60 ) ) )
{
// Do the deletion
unlink($path.$file);
}
}
}
}
Thank you in advance for your response.
With Iterators:
$days = 1;
$fsi = new RegexIterator(
new FilesystemIterator('/path/to/your/files'),
'(.+_\d+\.jpg$)'
);
/** #var SplFileObject $file */
foreach ($fsi as $file) {
if ($file->isFile() && $file->getMTime() < strtotime("-$days day")) {
unlink($file);
}
}
Functional approach:
$days = 1;
array_map(
function($file) use ($days) {
if (!is_dir($file) && filemtime($file) < strtotime("-$days day")) {
unlink($file);
}
},
glob('/path/to/your/files/*_[1-9].jpg')
);
Good old imperative:
$days = 1;
foreach (glob('/path/to/your/files/*_[1-9].jpg') as $file) {
if (!is_dir($file) && filemtime($file) < strtotime("-$days day")) {
unlink($file);
}
};

Related Links

Why is my POST an undefined array?
Laravel Eloquent Update with foreach
PHP: Why doesn't my code Insert to database
Refresh PHP session variables
Toggling with variables in PHP
update a JSON file using PHP/MYSQL - limit size issue
Lumen unpredictable output
Determine the end of a non-sequential multi-dimensional array
PHP: Finding duplicates in multidimensional arrays and echo'ing the result
Creating user in Laravel 5
MYSQL get data BETWEEN AND condition OR condition
Space and special characters problems on post
Laravel echo strange characters
Can I chain a variable number of query scopes using Laravel 5
Presenting an integer in a non standard money format with PHP
Looping name attribute[] in PHP to INSERT into database from JSON

Categories

HOME
orientdb
opencart
loopbackjs
android-emulator
moodle-api
onvif
fireloop
fbloginview
spring-amqp
squid
twisted
cpu-usage
activeadmin
sharedpreferences
substring
restier
router
password-generator
sap-lumira
ruby-daemons
math.js
acrobat
ccavenue
spring-annotations
openbr
intune
location-services
cucumber-junit
appcode
gsp
redgate
spinner
ms-solver-foundation
android-alarms
cloudera-quickstart-vm
stat
right-click
minikube
hibernate-ogm
openstack-glance
webvtt
mura
deedle
network-protocols
ltrace
sidewaffle
oid
system.net.mail
android-4.2-jelly-bean
android-syncadapter
seadragon
univocity
onepage-checkout
textpattern
self
opencyc
auto-generate
abstract-factory
ipod
web-api
prism-4
text-align
python-curses
nsmutabledata
mod-auth-openidc
sql-processor
sunos
microsoft-expression-web
string.format
xml-signature
windows-rt
xhprof
jquery-mobile-flipswitch
quantify
nidaqmx
thruway
particle-swarm
iirf
void-pointers
dynamic-rdlc-generation
ntlmv2
story
image-formats
pcspim
prefuse
dropshadow
kext
kernel32
tablet-pc
asp.net-mvc-views
3des
mygeneration
ccnet-config
ixmldomdocument
self-tracking-entities
.net-services
rational-unified-process
acceleration
activex-exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile