Jump to content

Archived

This topic is now archived and is closed to further replies.

mhoctober

File_exists and wildcards

Recommended Posts

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..

Share this post


Link to post
Share on other sites
I didn't have time to test it, but this should find files whose names contain your search criteria.

[code]
<?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);
}
?>

[/code]

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

Share this post


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

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=370975:date=May 3 2006, 01:01 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 3 2006, 01:01 PM) [snapback]370975[/snapback][/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:
[code]<?php
$chk_files = glob('myfiles/12334*.jpg');
if (emtpy($chk_files)) echo 'No files found';
?>[/code]

Ken
[/quote]


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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.