Sam San
Sam San

Reputation: 6903

PHP skip case sensitivity in file extension

I have rows on my database table which some are not synchronized on the directory.

ex. on my table I have

image.png

but on my directory I have

image.PNG

now, Im having a problem to check if

file_exist

because of the case sensitivity. I can't choose the option to synchronize my database and my files on the directory manually because it is too many.

May I know how can I used the file_exist that ignores the sensitivity of file types?

Upvotes: 0

Views: 3289

Answers (3)

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

As i think, your root cause is due to following--
If your database have "image.png" and in directory "image.PNG" means there is some problem with your insert query.
It must be same in both the places. It better to advice to store file name in lowercase as Linux systems are case sensitive.

Upvotes: 1

Jonah
Jonah

Reputation: 16222

For a more general solution, see the comments in the php docs:

General Solution

Which offers this version of the function:

/**
 * Alternative to file_exists() that will also return true if a file exists
 * with the same name in a different case.
 * eg. say there exists a file /path/product.class.php
 *  file_exists('/path/Product.class.php')
 *   => false
 *  similar_file_exists('/path/Product.class.php')
 *   => true
 */
function similar_file_exists($filename) {
  if (file_exists($filename)) {
    return true;
  }
  $dir = dirname($filename);
  $files = glob($dir . '/*');
  $lcaseFilename = strtolower($filename);
  foreach($files as $file) {
    if (strtolower($file) == $lcaseFilename) {
      return true;
    }
  }
  return false;
}

Upvotes: 4

heikkim
heikkim

Reputation: 2975

Create a function that checks both extensions and use it instead of file_exist directly.

Upvotes: 2

Related Questions