问题
I need to pass filenames via the url, e.g.:
http://example.com/images/niceplace.jpg
The problem I'm having is when the file name contains a blank character, e.g.:
http://example.com/images/nice place.jpg
or
http://example.com/images/nice%20place.jpg
For these two URLs, codeigniter complains about the blank char: "The URI you submitted has disallowed characters."
How should I go about fixing this?
I know I can add the blank character to the permitted_uri_chars
in config.php
but I'm looking for a better solution as there might be other disallowed characters in a filename.
回答1:
You can use this method:
http://php.net/urlencode
Actually, you will run into another issues, when a filename would contain &
character, and a few others. urlencode would get rid of all the possible issues.
回答2:
This configuration option is created to avoid some characters being passed in URI and you want to walkaround it in some cases. I think most appropriate solutions are:
- Pass file name as a parameter - http://domain.com/images/?image=test.jpg
- Remove all non alfanumeric characters and may be some other (dash, underscore, etc) from file name when you save it. In my opinion, it is better, because you can face other problems with some character in other cases.
回答3:
I figured out a solution.
The URL is generated using rawurlencode()
.
Then, within the images
controller, the filename is decoded using rawurldecode(html_entity_decode($filename))
.
I successfully tested this solution with a few special characters I can think of and with UTF-8 characters.
回答4:
One of the better way to work with url's for specified condition is to encode/encrypt your url parameters using encryption/security class in order to maintain URL security:
$encrypt=$this->encrypt->encode($param1) & $this->encrypt->decode($encrypt)
Alternatively if you want special chars to be allowed in the URL then change your config settings in config.php file.
File Location: application/config/config.php
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
Add all characters in right side that you want to be allowed with your application.
来源:https://stackoverflow.com/questions/25798572/how-to-pass-a-filename-via-url