By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

method="POST" changes '.' to '_'

P: n/a
I am trying to use POST to transfer data to another page. When I do
this, '.' characters get converted to"_". For example:

#index.html:
<form action="test.php" method="post">
<input type="submit" name="filename.txt">
</form>

#test.php
<html>
<?php
var_dump( $_POST );
?>
</html>

This displays:

array(1) { ["filename_txt"]= string(12) "Submit Query" }

ie 'filename.txt' is changed to 'filename_txt'
How can I stop this behaviour?

Jul 6 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On 6 Jul 2006 15:07:59 -0700, "Robert S" <ro**********************@gmail.com>
wrote:
>I am trying to use POST to transfer data to another page. When I do
this, '.' characters get converted to"_". For example:

#index.html:
<form action="test.php" method="post">
<input type="submit" name="filename.txt">
</form>

#test.php
<html>
<?php
var_dump( $_POST );
?>
</html>

This displays:

array(1) { ["filename_txt"]= string(12) "Submit Query" }

ie 'filename.txt' is changed to 'filename_txt'
How can I stop this behaviour?
You'd have to patch PHP.

See main/php_variables.c, php_register_variable_ex
(line 92 in PHP 5.1.4):

/* ensure that we don't have spaces or dots in the variable name (not binary
safe) */
for (p = var; *p; p++) {
if (*p == ' ' || *p == '.') {
*p='_';
} else if (*p == '[') {
is_array = 1;
ip = p;
*p = 0;
break;
}
}
var_len = p - var;

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jul 6 '06 #2

P: n/a
I am trying to use POST to transfer data to another page. When I do
this, '.' characters get converted to"_". For example:
You'd have to patch PHP.
Thanks for the speedy answer.

Is there another way? I'm using debian on a work PC and am extremely
reluctant to do any patching.

Jul 6 '06 #3

P: n/a
*** Robert S escribió/wrote (6 Jul 2006 15:34:38 -0700):
Is there another way? I'm using debian on a work PC and am extremely
reluctant to do any patching.
Why don't do simply perform some sort of encoding and decoding, like
bin2hex() or base64_encode() or even a custom one?

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Jul 6 '06 #4

P: n/a
On 6 Jul 2006 15:34:38 -0700, "Robert S" <ro**********************@gmail.com>
wrote:
>I am trying to use POST to transfer data to another page. When I do
this, '.' characters get converted to"_". For example:
You'd have to patch PHP.

Thanks for the speedy answer.

Is there another way? I'm using debian on a work PC and am extremely
reluctant to do any patching.
If you're brave, then there's $HTTP_RAW_POST_DATA, but since you'll then have
to do your own POST-data decoding then beware of getting it wrong.

Just going back to your original post:
<form action="test.php" method="post">
<input type="submit" name="filename.txt">
</form>
Are you committed to this layout? "filename.txt" can exist fine as a value,
it's just as a name it's a problem. Can you not send the filename over as a
value in an input type=hidden field, for example?

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jul 6 '06 #5

P: n/a
umm why dont you just (on the test.php page) do a str replace and
change the '_' back to a '.' ??

flamer.

Robert S wrote:
I am trying to use POST to transfer data to another page. When I do
this, '.' characters get converted to"_". For example:

#index.html:
<form action="test.php" method="post">
<input type="submit" name="filename.txt">
</form>

#test.php
<html>
<?php
var_dump( $_POST );
?>
</html>

This displays:

array(1) { ["filename_txt"]= string(12) "Submit Query" }

ie 'filename.txt' is changed to 'filename_txt'
How can I stop this behaviour?
Jul 6 '06 #6

P: n/a
>
Why don't do simply perform some sort of encoding and decoding, like
bin2hex() or base64_encode() or even a custom one?
Thanks. That looks like the best idea. Fortunately the program that
creates the files seems to give the files "safe" filenames, except for
the '.' with the extension. I might just trim off the extension and
add it at the other end. bin2hex also looks promising.

Jul 6 '06 #7

P: n/a
Geoff wrote:
On 6 Jul 2006 16:03:14 -0700, flamer di******@hotmail.com wrote...
>>umm why dont you just (on the test.php page) do a str replace and
change the '_' back to a '.' ??

flamer.


Thought about that too, but could have a problem with

<input type="submit" name="file_name.txt">

becoming "file.name.txt", if other form fields are using that logic.

Saw someone else (Andy?) mention the "name" being the problem and not the value.
Instead of messing around with the ".txt", name it "filename" and append a
".txt" later if it's that crucial to the program.
Geoff
Geoff,

Remember - the name attribute is just something for you to identify the
data when it's passed. It does not have to be an actual filename - it
could be "xyz" for that matter. But it HAS to be changed, because '.'
is not a valid character in a PHP variable.

Whatever you have here is the identifier in the $_POST or $_GET array,
i.e. $_POST['xyz'].

The value in the field is where you want "filename.txt", and that should
not be changed.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 7 '06 #8

P: n/a
Robert S wrote:
<form action="test.php" method="post">
<input type="submit" name="filename.txt">
</form>
Why are you even passing the filename in a button's name?

Why not:

<form action=test.php method=post>
<div>
<input type=hidden name=myfile value="filename.txt">
<input type=submit>
</div>
</form>

??

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jul 7 '06 #9

P: n/a
Why are you even passing the filename in a button's name?
>
Why not:

<form action=test.php method=post>
<div>
<input type=hidden name=myfile value="filename.txt">
<input type=submit>
</div>
</form>
The page is a fax client that allows large numbers of files to be faxed
off to multiple recipients. I have a whole lot of buttons and text
inputs that refer to different files. The receiving page loops through
$POST and identifies the file that the button refers to. All text
inputs and checkboxes etc need to be sent to the receiving page, so I
can't use multiple <formelements. The example I gave was just an
illustrative example - its not from my code. As far as I can see this
seems to be the only way of doing it. The files are .PDF files created
by cups-pdf - so they are FULL of underscores. If I use an <a href>
tag, the POST variables don't get sent to the receiving page.

bin2hex looks like the way to go.

Jul 7 '06 #10

P: n/a

Robert S wrote:
Why are you even passing the filename in a button's name?

Why not:

<form action=test.php method=post>
<div>
<input type=hidden name=myfile value="filename.txt">
<input type=submit>
</div>
</form>

The page is a fax client that allows large numbers of files to be faxed
off to multiple recipients. I have a whole lot of buttons and text
inputs that refer to different files. The receiving page loops through
$POST and identifies the file that the button refers to. All text
inputs and checkboxes etc need to be sent to the receiving page, so I
can't use multiple <formelements. The example I gave was just an
illustrative example - its not from my code. As far as I can see this
seems to be the only way of doing it. The files are .PDF files created
by cups-pdf - so they are FULL of underscores. If I use an <a href>
tag, the POST variables don't get sent to the receiving page.

bin2hex looks like the way to go.
In my mind if you cannot avoid having those periods in the form-fields,
you have a design problem in your code. For example:

<input type="hidden" name="filename1" value="filename1.txt">
<input type="submit" name="SubmitStuff1">

<input type="hidden" name="filename2" value="filename2.txt">
<input type="submit" name="SubmitStuff2">

With this kind of stuff on the target page you get the filename by:

if( isset($_POST["SubmitStuff1"])) {
$MyFilename = $_POST["filename1"];
}

or

for($i=0;$i<100;$i++) {
if(isset($_POST["SubmitStuff".$i)) {
$MyFilename = $_POST["filename".$i];
}
}

and so forth. You never really need a period in the field name, unless
the form is produced by some other software, generator, or some
framework, but these are other issues then.

Jul 7 '06 #11

P: n/a
Robert S wrote:
>>Why are you even passing the filename in a button's name?

Why not:

<form action=test.php method=post>
<div>
<input type=hidden name=myfile value="filename.txt">
<input type=submit>
</div>
</form>


The page is a fax client that allows large numbers of files to be faxed
off to multiple recipients. I have a whole lot of buttons and text
inputs that refer to different files. The receiving page loops through
$POST and identifies the file that the button refers to. All text
inputs and checkboxes etc need to be sent to the receiving page, so I
can't use multiple <formelements. The example I gave was just an
illustrative example - its not from my code. As far as I can see this
seems to be the only way of doing it. The files are .PDF files created
by cups-pdf - so they are FULL of underscores. If I use an <a href>
tag, the POST variables don't get sent to the receiving page.

bin2hex looks like the way to go.
If you don't mind assuming that javascript is on then:

one of these:
<input type='hidden' name='myfile' value='' />

and many of (assuming form's name is 'myform'):
<input type='submit'
onclick='document.myform.myfile.value="filename.tx t"' />

-------------------------------------------------
Alternatively, assuming the buttons have the filename as their text,
just have many:

<input type='submit' name='myfile' value='filename1.txt' />
<input type='submit' name='myfile' value='filename2.txt' />
<input type='submit' name='myfile' value='filename2.txt' />
....

then $_POST['myfile'] should be the value field of the clicked submit
button.
Jul 7 '06 #12

P: n/a
Both of these suggestions look good. I'm using javascript already and
that looks like the most elegant method.

Jul 7 '06 #13

P: n/a
Robert S wrote:
>>Why are you even passing the filename in a button's name?

Why not:

<form action=test.php method=post>
<div>
<input type=hidden name=myfile value="filename.txt">
<input type=submit>
</div>
</form>


The page is a fax client that allows large numbers of files to be faxed
off to multiple recipients. I have a whole lot of buttons and text
inputs that refer to different files. The receiving page loops through
$POST and identifies the file that the button refers to. All text
inputs and checkboxes etc need to be sent to the receiving page, so I
can't use multiple <formelements. The example I gave was just an
illustrative example - its not from my code. As far as I can see this
seems to be the only way of doing it. The files are .PDF files created
by cups-pdf - so they are FULL of underscores. If I use an <a href>
tag, the POST variables don't get sent to the receiving page.

bin2hex looks like the way to go.
OK, no problem. Just:

<form action=test.php method=post>
<input type=hidden name="filename[0]" value="filename.txt">
<input type=submit name="Submit[0]">
<input type=hidden name="filename[1]" value="filename.pdf">
<input type=submit name="Submit[1]">
<input type=hidden name="filename[2]" value="filename.doc">
<input type=submit name="Submit[2]">
<input type=hidden name="filename[3]" value="filename.html">
<input type=submit name="Submit[3]">
</form>

File names are in $_POST['filename[0]'], $_POST['filename[1]'], etc.
The button which was pressed will be in $_POST['Submit[x]'].

Simply loop through the Submit array to find which one was pressed with
isset(), and pick the appropriate file from the filename array.

Expandable, easily programmed and requires no JS.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 7 '06 #14

P: n/a
>
OK, no problem. Just:

<form action=test.php method=post>
<input type=hidden name="filename[0]" value="filename.txt">
<input type=submit name="Submit[0]">
<input type=hidden name="filename[1]" value="filename.pdf">
<input type=submit name="Submit[1]">
<input type=hidden name="filename[2]" value="filename.doc">
<input type=submit name="Submit[2]">
<input type=hidden name="filename[3]" value="filename.html">
<input type=submit name="Submit[3]">
</form>

Simply loop through the Submit array to find which one was pressed with
isset(), and pick the appropriate file from the filename array.
I've gone for this approach. Works a treat. Many thanks for the help
folks.

Jul 7 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.