How to pass a filename via URL?

不打扰是莪最后的温柔 提交于 2021-02-10 14:26:50

问题


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:

  1. Pass file name as a parameter - http://domain.com/images/?image=test.jpg
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!