Solved

How can i delete certain files from php in a directory

Posted on 2016-09-07
7
43 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
7 Comments
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
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
Comment Utility
I can delete them manually so I think it is fine
0
 
LVL 42

Expert Comment

by:zephyr_hex
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:Jasmine Ikhreishi
Comment Utility
Okayy thanks
0
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 250 total points
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 250 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now