phpfile-existsfilemtime

PHP file_exists, filemtime, stack, not work, but file exits,


I need to read some paths from a csv file and show it in the browser.

I can read the file with

file_get_contents.

But the encoding from the csv file is wrong. So i need to use

mb_convert_encoding($array[$i], 'UTF-8', 'UTF-16');

to get the correct path.

After this, i display the filename and the path with a tag

echo "<a href='" . $encoded_path . "'>" . $only_filname ."</a>";

When i click on the link, the file will be open. It works great!

At the end i need to show the last modified date. Here is my problem I wrote

echo "Content last changed: ".date("F d Y H:i:s.", filemtime($encoded_path));

i got the wrong date.... Each date was "1970"

what can be wrong?

I tried to use a other function

stat() -> mtime

it didn't work. that was the reason, why i would like to check if the file exists...

I tried this with if (file_exists())

so i saw, that the file ist not founded

i try to change the path, to a absolute path ->not help

my path look like this

from the csv file, i got this:

$path = "files/directory1/my file.pdf";

i tried to use

$newpath =  "../../files/directory1/my file.pdf" // not work
$newpath =  "c:/xampp/htdocs/web/files/directory1/my file.pdf" // not work
$newpath =  __DIR__ . $path; //not work
$newpath =  $_SERVER['DOCUMENT_ROOT'] . $path; //not work

the path works, if i copy the output of each version and paste it into the windows explorer the path works in a tag too.

if i show the path, select it with the mouse and copy paste it to the file_exists function it works too $newpath = "../../files/directory1/my file.pdf"; echo $newpath;

if(file_exist("../../files/directory1/my file.pdf")) // works

only file_exists and the other filetime functions not work

Thank you


Solution

  • Non-breaking space

    There is a character in UTF-8 and UTF-16 which looks identical the space character, but is actually a completely different character altogether: https://www.compart.com/en/unicode/U+00A0

    The non-breaking space character is visually indistinguishable from a space character, but will not be converted when you try to use str_replace to replace all spaces, because it has a different code point.

    The non-breaking space comes up mainly in situations like yours, when you're copying data from a sophisticated source, like a whitespace-safe CSV file, or a website that uses &nbsp; spaces.

    Here's an explanation of the character, and why it's useful: https://en.wikipedia.org/wiki/Non-breaking_space

    How to prevent the issue in the future

    Right after you've fixed up the character encoding, add a line like this:

    $encoded_path = str_replace("\u{a0}", ' ', $encoded_path);
    

    This will convert all non-breaking spaces to a regular spaces.

    Or, if you'd just like to remove that one trailing space and be done with it, you can do that directly with the trim function:

    $encoded_path = trim($encoded_path, " \u{a0}");