-1

I have image uploaded on the server with the id of customer. But I don't want to upload multiple format of files twice (e.g. there should be only one image for customer id=1 that is 1.jpg OR 1.png)

How can I check globally while updating image whether that file is already there or not?

can I check file exist without extension of file?

I am using this command to check file.

file_exists('./media/customer-id/'.$cus_id);
user3609998
  • 63
  • 1
  • 1
  • 7
  • 1
    Check the [duplicate answer](http://stackoverflow.com/questions/3303691/php-check-file-exists-without-knowing-the-extension#answer-3303718) from the link I posted. You'll need to use `glob()`. [This comment](http://stackoverflow.com/questions/3303691/php-check-file-exists-without-knowing-the-extension#comment-3422841) by _@Gumbo_ answers your question exactly. Make sure you search the site to avoid posting a duplicate question. – War10ck May 23 '14 at 21:11

2 Answers2

1

You could use glob() for this and count the result.

function patternExists($pattern) {
    return count(glob($pattern)) > 0;
}

and use like this

if (patternExists("./media/customer-id/".$cus_id."*")) {
    // bad!
}
kero
  • 10,389
  • 5
  • 38
  • 47
  • 2
    Note that `glob()` is somewhat of a quasi-regex. The `?` matches 1 of any character except a `/` and `*` matches 0 or more of any character except a `/`. +1 for a simple solution, though. – Sam May 23 '14 at 21:12
  • 1
    @Sam thx, I've renamed it to use PHP's formulation – kero May 23 '14 at 21:13
  • @kingkero: how to see that image in browser without knowing it's extension. – user3609998 May 23 '14 at 21:28
0

Use scandir() to run a ls like command and receive an array of the directories contents. Then loop through the files and see if anything matches the customer ID.

$exists = false;
foreach(scandir('./media/customer-id') as $file) {
    if(preg_match('/^' . $customer_id . '\.$/', $file)) {
        $exists = true;
        break;
    }
}
Sam
  • 18,756
  • 2
  • 40
  • 65