Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can i delete certain files from php in a directory

Posted on 2016-09-07
7
Medium Priority
?
65 Views
Last Modified: 2016-09-09
I want to deletes files in my date('m-d-Y').'/' directory that contain ".5010." in there filename

Is it by:
$filename= "%5010%";
unlink(filename,path)

Open in new window


Because this didnt work?
0
Comment
Question by:Jasmine Ikhreishi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41787839
You should check to make sure you have permissions to delete the file using is_writable()

is_writable ( string $filename )

Open in new window


Where $filename includes the full path to the file.
1
 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41787842
I can delete them manually so I think it is fine
0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41787846
But you are not PHP.  You need to check whether it works from PHP because it's PHP that's doing the deleting, not you.
1
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
ID: 41787854
Okayy thanks
0
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 1000 total points
ID: 41787886
The unlink() function doesn't support wildcard.
One way to delete all files with wildcard is to use the glob function.  In your case:
<?php
foreach (glob("*.5010.*") as $filename) {
   unlink($filename);
}
?>

Open in new window

or you can use one of system(), exec() or shell_exec()
1
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 1000 total points
ID: 41787906
unlink works on one file at a time so you will need to iterate over all files in the folder, check if they match
This is the simplest - not the fastest - method of doing it

$folder = 'path/to/files/';
foreach(glob($folder . '*5010*') as $path) {
   if (is_file($path) && is_writable($path)) {
     unlink($path);
   }
}

Open in new window

1

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question