Special Characters in "File_Exists" Problem (Php)

special characters in file_exists problem (php)

What's the server OS?

If it's Windows, you'll not be able to access files under a UTF-8-encoded filename, because the Windows implementation of the C IO libraries used by PHP will only talk in the system default code page. For Western European installs, that's code page 1252. You can convert a UTF-8 string to cp1252 using iconv:

$winfilename= iconv('utf-8', 'cp1252', $utffilename);

(utf8_decode could also be used, but it would give the wrong results for Windows's extension characters that map to the range 0x80-0x9F in cp1252.)

Files whose names include characters outside the repertoire of the system codepage (eg. Greek on a Western box) cannot be accessed at all by PHP and other programs using the stdio. There are scripting languages that can use native-Unicode filenames through Win32 APIs, but PHP5 isn't one of them.

And of course the step above shouldn't be used when deployed on a different OS where the filesystem is UTF-8-encoded. (ie. modern Linux.)

If you need to seamlessly cross-server-compatible with PHP, you'll have to refrain from using non-ASCII characters in filenames. Sorry.

file_exists cannot handle the file name that has spacial character

Try realpath

The realpath() function returns the absolute pathname.

This function removes all symbolic links (like '/./', '/../' and extra
'/') and returns the absolute pathname.

realpath() returns FALSE on failure, e.g. if the file does not exist.

Can't escape the '&' in a filename such that file_exists evaluates to true

My bet is that it's not actually PHP with this issue, as & is not a special character for PHP, and given the error it actually appears to be the space at issue. While space and & are not special characters in PHP, they are in a URL. So, I suspect what is happening is your URL is something like

http://www.example.org/script.php?name=A & T.pdf

This would need to be URL encoded

http://www.example.org/script.php?name=A%20%26%20T.pdf

PHP has a command you can use if you're setting up the URL with it, otherwise do some googling for online URL encoders: https://www.php.net/manual/en/function.urlencode.php

Special characters problem when displaying directory contents with PHP

Are you using IIS to host this PHP script?
If yes, maybe this problem can be solved changing some IIS configurations, following the instructions from this link.

If possible, normalize the filenames in the server to a more legible way, like changing "space" to "_", "?" to "", "ç" to "c", and so on.

I hope it helps...

PHP file_exists with accent returns false

This works like charm

<?php
$dir = 'D:\wamp\www\test\data\Folderé';
var_dump(file_exists((utf8_decode($dir))));

file_exists() for a file that contains (&) in filename

The script seems to work for me.

Make sure that the file you're checking is in the directory where your script is run.

use getcwd() to see where your script is running.

e.g. if your script is in my/scripts/script.php
but if it's included and invoked by another script at my/other/scripts/index.php then your script would actually be running in the my/other/scripts directory, NOT in the my/scripts directory as you might expect



Related Topics



Leave a reply



Submit