Solved

How can i delete certain files from php in a directory

Posted on 2016-09-07
7
50 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 (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 42

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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 108

Expert Comment

by:Ray Paseur
ID: 41789624
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache/PHP: Cannot make phpinfo() and phpmyadmin work 4 34
phpmailer in WHILE loop - weird results 10 27
get domain with php 7 19
PHP get array item with custom id 4 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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…

910 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

19 Experts available now in Live!

Get 1:1 Help Now