emrea
emrea

Reputation: 1355

How to pass a filename via 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

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.

Upvotes: 1

Views: 2889

Answers (4)

k10gaurav
k10gaurav

Reputation: 462

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.

Upvotes: 0

emrea
emrea

Reputation: 1355

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.

Upvotes: 1

Timur
Timur

Reputation: 6718

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.

Upvotes: 0

czaks
czaks

Reputation: 138

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.

Upvotes: 0

Related Questions