sbcjr
asked on
PHP Function to Find and Unlink Files Regardless of File Extension
Hi, I'm stumped on this one- I have a start here, but I think I may need to just scratch it and start over.
What I need to do is create a function to search a directory for all files with a given filename, regardless of their extension, and then delete them. For example, in the "docs" directory, find any files with the filename "Resume_1", and it might return "Resume_1.doc", "Resume_1.docx", "Resume_1.ppt", and so on. All of these files should be deleted.
Anyway... here's what I have, but it's obviously incorrect. :)
I'd be extremely grateful for any help!
What I need to do is create a function to search a directory for all files with a given filename, regardless of their extension, and then delete them. For example, in the "docs" directory, find any files with the filename "Resume_1", and it might return "Resume_1.doc", "Resume_1.docx", "Resume_1.ppt", and so on. All of these files should be deleted.
Anyway... here's what I have, but it's obviously incorrect. :)
I'd be extremely grateful for any help!
function find($dir,$pattern){
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$posDec = strpos($file,".");
$fileName = substr($file,0,$posDec);
if($fileName == $pattern){
array_push($files,$file);
}
}
}
}
closedir($handle);
return $files;
}
$dir = "../files/docs";
$fileName = "Resume_1";
$files = find($dir,$fileName);
$doc = $files['0'];
if (file_exists($doc)) unlink($doc);
Hi,
I think the problem might be that there is no path in the $filename given back from readdir();
Please try the following:
After:
$doc = $files['0'];
also give:
echo $doc
and see, if it is only the filename or the filename including the directory path. If the directory path is not included you have to do:
I think the problem might be that there is no path in the $filename given back from readdir();
Please try the following:
After:
$doc = $files['0'];
also give:
echo $doc
and see, if it is only the filename or the filename including the directory path. If the directory path is not included you have to do:
if (file_exists($dir . "/" . $doc)) unlink($dir . "/" . $doc);
Try this:
<?php
function find_files($dir,$pattern)
{
$files = array();
if (is_dir($dir))
{
//echo "is dir";
if ($dh = opendir($dir))
{
echo "Directory handle: $handle\n";
while (($file = readdir($dh)) !== false)
{
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
$tmp=explode(".",$file);
print_r($tmp[0]."<br>");
if ($tmp[0] == $pattern)
{
$files[] = $file;
}
}
closedir($dh);
}
}
else
{echo "not dir";}
return $files;
}
$dir = "../files/docs/";
$fileName = "test1";
$f_files = find_files($dir,$fileName);
print_r($f_files);
foreach($f_files as $files)
{
echo "deleting file:".$dir.$files;
unlink($dir.$files);
}
php?>
I couldn't really see much of a problem with your code, but have tidied it up a little.
Note:
1) The file which are to be deleted will have to have the right permissions so that the user the web server is runnning as can delete them.
2) that I'd set $dir as a full system path not a relative directory path
Note:
1) The file which are to be deleted will have to have the right permissions so that the user the web server is runnning as can delete them.
2) that I'd set $dir as a full system path not a relative directory path
function find($dir,$pattern){
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$posDec = strpos($file,".");
$fileName = substr($file,0,$posDec);
if($fileName == $pattern ){
unlink($file);
}
}
}
}
closedir($handle);
}
$dir = "./";
$fileName = "example";
find($dir,$fileName);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use base name instead of explode
<?php
function find_files($dir,$pattern)
{
$files = array();
if (is_dir($dir))
{
echo "is dir";
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
if (basename($file) == $pattern)
{
$files[] = $file;
}
}
closedir($dh);
}
}
else
{echo "not dir";}
return $files;
}
$dir = "/var/www/testarea/delete_files/";
$fileName = "test1";
$f_files = find_files($dir,$fileName);
foreach($f_files as $files)
{
echo "deleting file".$dir.$files;
unlink($dir.$files);
}
php?>
Sorry got that a little wrong, inside the first while loop in the function it should read:
$info = pathinfo($file);
if (basename($file,'.'.$info['extension']) == $pattern)
{
$files[] = $file;
}
ASKER
Absolutely superb! Works perfectly!!
Thank you *all* very, very much for taking the time to respond and help me out. I very sincerely appreciate it.
Thank you *all* very, very much for taking the time to respond and help me out. I very sincerely appreciate it.
ASKER