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

Avatar of undefined
Last Comment
ddrudik

8/22/2022 - Mon
Loganathan Natarajan

apply strtolower()
Loganathan Natarajan

i mean to the file names? before the file_exists()
ASKER
Desire2change

the  physical filenames can b having lower or upper, so if iam going to apply strtolower, in the code , how is it correct ??
Your help has saved me hundreds of hours of internet surfing.
fblack61
albrieu

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

albrieu

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

ddrudik

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
albrieu

in windows the function file_exists not ignore the case
ddrudik

albrieu, possibly that varies by version, note this comment regarding case/Win32:
http://us2.php.net/manual/en/function.file-exists.php#74159
ddrudik

albrieu, it is case-insensitive on my system - PHP 5.2.6 Windows NT WWW 5.2 build 3790.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
albrieu

ddrudi, in my system it´s case sensitive

Windows NT 5.1 build 2600
PHP Version 5.2.6
albrieu

ddrudik: you use IIS server?
ddrudik

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ddrudik

On Win32 I tested on IIS/Windows 2003 Server.
ddrudik

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

albrieu

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

bye
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ddrudik

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
albrieu

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
albrieu

sorry  ddrudik:

file_get_content()
albrieu

and works fine!!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ddrudik

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