开发者

PHP file_get_contents changing file name

开发者 https://www.devze.com 2023-04-08 10:20 出处:网络
I need to get a file from the server and I\'m doing it like this: file_get_contents($path.$fileName); It works well with most of the files, until I started experiencing some issues in particular ca

I need to get a file from the server and I'm doing it like this:

file_get_contents($path.$fileName);

It works well with most of the files, until I started experiencing some issues in particular cases like the following:

Where the $path is a string like this: "/path/to/app/and/file/folder/" (not a url but a realpath)

and $fileName is: "PCard__0000_Front_(2).jpg"

The error I get is:

/path/to/app/and/file/folder/PCard__0000_Front_(2).jpg)
[function.file-get-contents]: failed to open stream: No such file or
direc开发者_如何学Gotory

Now if I call the function with a string: file_get_contents("/path/to/app/and/file/folder/PCard__0000_Front_(2).jpg") it works well

Please some advise on what am I doing wrong, I would really like to understand why this happens.

Thanks!

P.S. I have seen some others say to use curl instead but I would really appreciate if someone could shed some light into why this is happening and how can I solve it using file_get_contents no matter how much better the other function could be.


The error message is clear and obvious.

There is no such file /path/to/app/and/file/folder/PCard__0000_Front_(2).jpg

So, for some reason you are changing the filename from PCard__0000_Front_(2).jpg to PCard__0000_Front_(2).jpg.

A solution: do not change the filename.


Judging by the output of the error message with the filename showing, PCard__0000_Front_(2).jpg

perhaps there's a need for urldecode($filename)


SOLUTION

As suggested by a comment (thank you) the solution for the proper file name would be

html_entity_decode($filename)
0

精彩评论

暂无评论...
验证码 换一张
取 消