$_GET['type'] returns truncated folder name due to ampersand
Moderator: General Moderators
$_GET['type'] returns truncated folder name due to ampersand
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.
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
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 &.
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 &.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: $_GET['type'] returns truncated folder name due to amper
Did you try it urlencoded like: http://OurServerName/FCS/Planning%20%26 ... evelopment
(#10850)
Re: $_GET['type'] returns truncated folder name due to amper
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.
Frankly, I'm not sure what you mean by "URL rewriting rules", but what I'm getting from is incorrect at that point without me manipulating it in any way. I'm not formatting it or anything like that at that point.
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.
One of the first codes statements is
Basically everything comes from there. Since 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.
Thanks, again, for the reply.
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']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'];
Code: Select all
$_GET['type']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
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.Christopher wrote:Did you try it urlencoded like: http://OurServerName/FCS/Planning%20%26 ... evelopment
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
I was asking about URL rewriting because the URL you're trying to go to
doesn't have a ?type= in there. So there must be something, somewhere, which is "rewriting" it into a form more like
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.
Code: Select all
http://OurServerName/CS/Planning & Program DevelopmentCode: Select all
http://OurServerName/CS/some_script.php?type=Planning & Program DevelopmentChris'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
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> ";
Thanks, again!
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> ";
Thanks, again!
Re: $_GET['type'] returns truncated folder name due to amper
can you try to create folders named Program Development with mkdir("Program Development") or mkdir("Program\ Development")
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: $_GET['type'] returns truncated folder name due to amper
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.
(#10850)