Page 1 of 1

$_GET['type'] returns truncated folder name due to ampersand

Posted: Wed Feb 11, 2015 4:20 pm
by Ventris
This is my first post. I am basically a PHP novice, and I'm using some code mostly developed by someone else. The general purpose of the code is to generate links dynamically to folders and subfolders and files. It works great except for folders with an ampersand (&) in the name, which is causing $_GET['type'] to truncate the name at the ampersand's location.

I realize, now, that just because Microsoft will let you name folders with various characters doesn't mean that you should. I would just rename the folder, but our users have access to various folders in which they can create, rename, and delete folders and subfolders as they wish. So, I have to have a solution that allows for that.

This statement:

$CurrentFolderName = $_GET['type'];

Sets $CurrentFolderName to this:

http://OurServerName/CS/Planning

It should be this:

http://OurServerName/CS/Planning & Program Development

How do I get it to show the full folder name? I have searched Google and forums and haven't found the answer.

Thank you for the assistance. I will be checking for answers in the morning.

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Wed Feb 11, 2015 5:01 pm
by requinix
What are your URL rewriting rules? It may be rewriting to "/path/to/script.php?type=Planning & Program Development" instead of encoding the & (which generally happens automatically...).

And are you sure the $CurrentFolderName is "Planning " (maybe with a CS/ in front?) and not "Planning"? Note the trailing space on the first one.

What is your code that outputs the links? You need to make sure you're outputting a & and not a raw &.

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Wed Feb 11, 2015 5:26 pm
by Christopher

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Thu Feb 12, 2015 3:50 pm
by Ventris
Thank you very much for the reply, requinix. I'm sorry for not replying sooner. I'm slammed with other work today, and it's going to carry over to tomorrow, at this point.

requinix wrote:What are your URL rewriting rules? It may be rewriting to "/path/to/script.php?type=Planning & Program Development" instead of encoding the & (which generally happens automatically...).


Frankly, I'm not sure what you mean by "URL rewriting rules", but what I'm getting from

Code: Select all

$_GET['type']
is incorrect at that point without me manipulating it in any way. I'm not formatting it or anything like that at that point.

requinix wrote:And are you sure the $CurrentFolderName is "Planning " (maybe with a CS/ in front?) and not "Planning"? Note the trailing space on the first one.


Yes, it actually does have the ending space. I wrote a comment to the html showing the path and put quotes around it like this:

echo "<!-- \"\$CurrentFolderName = \$_GET['type'];\" is \"".$CurrentFolderName."\" -->\r\n";

You can see I didn't mistakenly put a space in the code to write the comment; it outputs with the space, though.

requinix wrote:What is your code that outputs the links? You need to make sure you're outputting a & and not a raw &.


One of the first codes statements is

Code: Select all

$CurrentFolderName = $_GET['type'];
Basically everything comes from there. Since

Code: Select all

$_GET['type']
isn't returning the path correctly, I think that is the source of the problem.

I did play around with urlencode, as Christopher suggested, before posting the question. As I mentioned, though, I think the problem comes directly from the

Code: Select all

$_GET['type']
.

Thanks, again, for the reply.

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Thu Feb 12, 2015 3:59 pm
by Ventris
Christopher wrote:Did you try it urlencoded like: http://OurServerName/FCS/Planning%20%26 ... evelopment
Thank you very much for the reply, Christopher. I'm sorry for not replying sooner. I'm slammed with other work today, and it's going to carry over to tomorrow, at this point.

Before posting the question, I did play around unsuccessfully with urlencode, but I think the problem comes directly from the $_GET['type'], which is key to the rest of the code. Could there be some other way to use $_GET in order to control what it returns? (It feels frustratingly like calling a stored procedure in a program, not getting the result I want, and having no way to correct it, because I don't have access to the code of the SP.)

Thanks for the suggestion.

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Thu Feb 12, 2015 5:02 pm
by requinix
I was asking about URL rewriting because the URL you're trying to go to

Code: Select all

http://OurServerName/CS/Planning & Program Development
doesn't have a ?type= in there. So there must be something, somewhere, which is "rewriting" it into a form more like

Code: Select all

http://OurServerName/CS/some_script.php?type=Planning & Program Development
That's how you're getting $_GET[type] (whatever value that may be) despite there not being a query string including a type=.


Chris's suggestion about urlencode() happens when you're generating the links, earlier. Not in the script where you're trying to get the value back out of the URL.

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Wed Feb 18, 2015 3:38 pm
by Ventris
requinix and Christopher,

Thank you both very much for your suggestions. I tried the urlencode and urldecode but couldn't make it work. My local expert was available again today and I just finished working through it with him to a solution. He also had suggested the urlencode/urldecode last week after I posted here. The solution was so close to what Christopher suggested with the urlencode. We used rawurlencode. Here are the two relevant lines of code. (He also thought it best for me to use DIRECTORY_SEPARATOR, so that was added as well, though I don't think that was required to solve the problem.)

$CurrentFolderName = rawurldecode($_GET['type']);

. . .

echo "<a href=\"".$_SERVER['PHP_SELF']."?type=".rawurlencode($DirectoryPathForGeneratingLinks.DIRECTORY_SEPARATOR.$Folder).""."\">".$Folder."</a>&nbsp;";

Thanks, again!

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Wed Feb 18, 2015 5:29 pm
by sky2002
can you try to create folders named Program Development with mkdir("Program Development") or mkdir("Program\ Development")

Re: $_GET['type'] returns truncated folder name due to amper

Posted: Wed Feb 18, 2015 6:05 pm
by Christopher
Interesting. The difference between urlencode and rawurlencode is how they handle spaces. urlencode converts them to +'s while rawurlencode encodes them. Not clear why it made a difference in your case, but glad you got it working.