Link to home
Start Free TrialLog in
Avatar of Desire2change
Desire2change

asked on

PHP- file_exists()

Hello,

I am chking if a file exists or not using
file_exists function.

Now,
the filename is abc.JPG

in the code , it is checking for abc.jpg

while actually it is abc.JPG, so a difference in case.

This is just an example, there are many files which uses this function and all are chking for '.jpg' while all the images in the system have '.JPG' and some have '.jpg'
The fileexist is returning false because of this case difference , whereas it should return true.

Can anyone suggest me an easy way, so that without changing much I can make the file_exists function case insensitive ??

Please answer asap.

Thanks.
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

apply strtolower()
i mean to the file names? before the file_exists()
Avatar of Desire2change
Desire2change

ASKER

the  physical filenames can b having lower or upper, so if iam going to apply strtolower, in the code , how is it correct ??
try this code


<?php
$path_parts = pathinfo('file.jpg');
 
if(file_exists($path_parts['dirname'].'/'.$path_parts['filename'].'jpg') or file_exists($path_parts['dirname'].'/'.$path_parts['filename'].'JPG'))
{
//do someting
}
 
?>

Open in new window

o try with this function

return the correct file name with a given lists of extensions if the file not in directory tthe function returns flase
<?php
$_exten=array();
$_exten[]='txt';
$_exten[]='TXT';
 
 
$_file_name=nfile_exists('filename.TXT',$_exten);
if($_file_name!=False)
{
	//show correct filename
	echo  $_file_name;
}else{
	echo 'Not in directory';
}
 
 
 function nfile_exists($file,$extensions=array()) {
    $path_parts = pathinfo($file);
	
	if(count($extensions)>0 and is_array($extensions))
	{
		foreach($extensions as $_extesn)
		{
			$_filename=$path_parts['dirname'].'/'.$path_parts['filename'].'.'.$_extesn;
			if(file_exists($_filename))
			{
				return $_filename;
			}
		}
	}
	return False;
}
 
 
 
?>

Open in new window

It would seem problematic if you choose to ignore case on UNIX system, since you could have 2 files with the same names but different case in the same directory (as far as I can tell).  On Windows, where that isn't possible, file_exists apparently ignores case.
in windows the function file_exists not ignore the case
albrieu, possibly that varies by version, note this comment regarding case/Win32:
http://us2.php.net/manual/en/function.file-exists.php#74159
albrieu, it is case-insensitive on my system - PHP 5.2.6 Windows NT WWW 5.2 build 3790.
ddrudi, in my system it´s case sensitive

Windows NT 5.1 build 2600
PHP Version 5.2.6
ddrudik: you use IIS server?
in the following example 1.php exists and the function returns "yes" on Unix and Win32.
<?php
function myfile_exists($file,$dir){
	return in_array($dir.strtolower($file),array_map('strtolower',glob("$dir*"))) ? "yes" : "no" ;
}
echo myfile_exists('1.PHP','./');
?> 

Open in new window

On Win32 I tested on IIS/Windows 2003 Server.
If you will always specify a directory with the file path you can simplify the function call to:
<?php
function myfile_exists($file){
	$path=pathinfo($file);
	$dir=$path['dirname']!='' ? $path['dirname'] : '.' ;
	return in_array(strtolower($file),array_map('strtolower',glob($path['dirname'].'/*'))) ? "yes" : "no" ;
}
echo myfile_exists('./1.PHP');
echo myfile_exists('./dir1/dir2/1.PHP');
?> 

Open in new window

ddrudik:
i use Apache thas is the diference in IIS returns filesystem like windows but in apache in wndows return like linux.

bye
albrieu, did you try the function I posted in 23492796?  You can have it return true / false instead of "yes" / "no" so it returns like the traditional file_exists function.
ASKER CERTIFIED SOLUTION
Avatar of albrieu
albrieu
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry  ddrudik:

file_get_content()
and works fine!!
albrieu, that should be file_get_contents() function, should be working fine for you, please confirm.