Jump to content


Photo

File_exists and wildcards


  • Please log in to reply
3 replies to this topic

#1 mhoctober

mhoctober
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 02 May 2006 - 08:03 PM

All,

my users load images, which I store and partly rename - I add a number to the front of the image name eg. myboata.jpg and myboatb.jpg become for instance 12334(1)myboata.jpg, 12334(2)myboatb.jpg etc...

I then want to search for all instances of files that commence with 12334 and am trying the following...

$pathAndFile = "myfiles/12334*"

if (file_exists($pathAndFile)) {
echo "The file exists";
} else {
echo "The file does not exist";
}

in the folder myfiles there are files that begin with 12334, but the message I get back from the If statement is telling me that "The File Does Not Exist"

I have also tried $pathAndFile = "myfiles/12334*.*"

and get the same result.

Is it legal to use wildcards in this way with the file_exists function?

If not - does anyone have any idea how I can serach for files where only the first 5 characters of the filename will ever be known?

Many thanks..

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 02 May 2006 - 08:42 PM

I didn't have time to test it, but this should find files whose names contain your search criteria.

<?php
$needle = "12345"; // this is the stuff (string/name/etc) we are looking for

if ($handle = opendir('/path/to/files')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $pos = strpos($file, $needle);
            if($pos === false) {
                // not found
            }else{
                // you found a file now do what you  want
            )
        }
    }
    closedir($handle);
}
?> 


Also you could modify the code to check if the first 5 character match the needle, by using the substr() function
Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 May 2006 - 06:01 PM

A much easier way of doing this would be to use the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob()[/a] function:
<?php
$chk_files = glob('myfiles/12334*.jpg');
if (emtpy($chk_files)) echo 'No files found';
?>

Ken

#4 mhoctober

mhoctober
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 May 2006 - 07:34 PM

[!--quoteo(post=370975:date=May 3 2006, 01:01 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 3 2006, 01:01 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
A much easier way of doing this would be to use the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob()[/a] function:
<?php
$chk_files = glob('myfiles/12334*.jpg');
if (emtpy($chk_files)) echo 'No files found';
?>

Ken
[/quote]


Fantastic ! Works a treat ! Thank God for this forum!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users