Strange behaviour of rawurlencode() ...how to get it to work?
I found quite annoying thing in my PHP project. My webpage can store files, and then enable users to download/show them. For that means I have made URL links (following to model classes handling the name of file and providing it, all works here)
But with files there comes the problem with their names, expecially spaces and special characters (which are quite common in language of the site). My system works on supplying the name of file by clicking the link (The download script kills all hotlinking tries, so only clickable links do the job).
So I got the problem with dealing with those special names of files. I am aware of rawurlencode()
function, which I wanted to use, however it behaves really strange. Here is my code: (I have it in foreach
loop for all files, so there goes the $key
, but I will show it for one index).
echo $this->files[$key];
//shows name of file: a č o.png
echo rawurlencode($this->files[$key]);
//shows translated name: a%20%C4%8D%20o.png
echo URL;
//gives (yes, it is local XAMPP test server) : http://127.0.0.1/
echo '<a href="' . URL . 'file/show/' . $this->files[$key] . '" target="_blank"> Show file </a>';
// gives link leading to: 127.0.0.1/file/show/a č o.png
That was expected, but this I don't get:
echo '<a href="' . URL . 'file/show/' . rawurlencode($this->files[$key]) . '" target="_blank"> Show file </a>';
//gives link leading to: 127.0.0.1/file%2Fshow%2Fa č o.png
$address = rawurlencode($this->files[$key]);
echo '<a href="' . URL . 'file/show/' . $address . '" target="_blank"> Show file </a>';
//gives link leading to: 127.0.0.1/file/show/a č o.png
Those encodes just ignore spaces&special
characters as ěščřžýáíéä
etc... my question is...What am I doing wrong and why sometimes this function works and sometimes does not? Why storing in $value
erases the encoding or that function does not work in echo unless it is alone in that echo?
Is it something wrong with anchor tag? Because of even this:
echo '<a href="' . URL . 'file/show/';
echo rawurlencode($this->files[$key]);
echo '" target="_blank"> Show file </a>';
//gives link leading to: 127.0.0.1/file/show/a č o.png
Again, no spaces&characters translated. Is my system haunted or anybody has a clue?
I also tried urlencode()
, that one translated spaces to + as expected, however It also couldn't deal with all the special characters. Is there any other function ?
I am using UTF-8 for all the things.
链接地址: http://www.djcxy.com/p/57952.html上一篇: 什么是'空合并'(?)运算符用于?