Solved

How can i delete certain files from php in a directory

Posted on 2016-09-07
7
61 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 43

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 43

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Assisted Solution

by:Brian Tao
Brian Tao earned 250 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 57

Accepted Solution

by:
Julian Hansen earned 250 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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41789624
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
geting data from the array list 6 37
Securing source code... theoretical question 10 48
Centos webserver sending tons of spam 27 34
Phone number mask 4 12
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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