如何通过URL传递文件名? [英] How to pass a filename via URL?

查看:69
本文介绍了如何通过URL传递文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过url传递文件名,例如:

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

http://example.com/images/nice%20place.jpg

对于这两个URL,codeigniter抱怨空白字符:您提交的URI不允许使用字符."

For these two URLs, codeigniter complains about the blank char: "The URI you submitted has disallowed characters."

我应该如何解决这个问题?

How should I go about fixing this?

我知道我可以将空白字符添加到 config.php 中的 permitted_uri_chars 中,但是我正在寻找更好的解决方案,因为在文件名.

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.

推荐答案

我想出了一个解决方案.

I figured out a solution.

URL是使用 rawurlencode()生成的.

The URL is generated using rawurlencode().

然后,在 images 控制器中,使用 rawurldecode(html_entity_decode($ filename))解码文件名.

Then, within the images controller, the filename is decoded using rawurldecode(html_entity_decode($filename)).

我使用一些我可以想到的特殊字符以及UTF-8字符成功地测试了该解决方案.

I successfully tested this solution with a few special characters I can think of and with UTF-8 characters.

这篇关于如何通过URL传递文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆