9

I have an URL like this

http://www.example.com/Data/image/office-dôn-sì-à.jpg 

I want to copy that file to my server using copy function in php. So the first thing is to encode it to this (I think browsers do the same thing)

http://www.example.com/Data/image/office-d%C3%B4n-s%C3%AC-%C3%A0.jpg 

But if I use function urlencode, full url will be encoded to

http%3A%2F%2Fwww.example.com%2FData%2Fimage%2Foffice-d%C3%B4n-s%C3%AC-%C3%A0.jpg 

which is not an URL anymore and not what I want.

Any help?

3
  • Do you want to downoad a file from an other server to yours using HTTP wrapper? php.net/manual/en/wrappers.php - How do you get the URL? Commented Mar 26, 2011 at 10:29
  • The encoded ULR is copied from address bar in FF. I did change its host name due to privacy. I want to copy file using: copy('http://www.example.com/Data/image/office-d%C3%B4n-s%C3%AC-%C3%A0.jpg', 'destination.jpg'); Commented Mar 26, 2011 at 10:53
  • If you are sure that only the last piece (the filename) will contain special characters you can use Russel's solution. Commented Mar 26, 2011 at 11:00

1 Answer 1

9

So, the other answers here have largely ignored your post, it seems. Let's hope I have not done the same.

It seems to me that you only want to encode the basename? If that is true, this ad-hoc function should do the trick:

function encode_basename($url) { $url = explode('/', $url); $base = array_pop($url); return implode('/', $url) . '/' . urlencode($base); } //returns: http://www.example.com/Data/image/office-d%25C3%25B4n-s%25C3%25AC-%25C3%25A0.jpg 
Sign up to request clarification or add additional context in comments.

3 Comments

function encode_basename($url) { $url = explode('/', $url); $base = array_pop($url); return implode('/', $url) . '/' . rawurlencode($base); }
This answer shouldn't be marked as accepted as it doesn't solve the question. As others pointed out, you cannot use urlencode() but rawurlencode().
No difference. This basename-only solution was not an answer anyway. And in this case, urlencode and rawurlencode are the same issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.