Jump to content

File_exists and wildcards


mhoctober

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..
Link to comment
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...
Link to comment
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
Link to comment
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.