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

multi-file upload - $_POST not working...

P: n/a
Ben
Hi everybody!
I have a multi-file upload form... Please looks at my code below. It's
in an initial stage. I'm trying to display the values of each text box
when I press "upload" but it doesn't seem to work... i don't know
what's the problem.. I'm using PHP5.

<html>
<head>
</head>
<body>
<?php
function ifcond($i) {
if ($i > 9) {
return $i;
}
else {
return '0'.$i;
}
} //end function ifcond()
for ($i=1; $i < 11; $i++) {
$image[$i - 1] = $_POST[`echo 'image'.ifcond($i);`];
}
print_r($image);
?>

<form action="<?php echo $_SERVER['../PHP_SELF']; ?>"
method="post">
<?php
for ($i=1; $i < 11; $i++) { ?>
Image<?php echo ifcond($i); ?>:
<input type="file" name="image"<?php echo ifcond($i); ?> /><br /><br
/>
<?php } ?>
<input type="submit" value="Upload" /></p></form>
</form>
</body>
</html>

Thanx!
Ben
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
*** Ben wrote/escribió (22 Sep 2004 23:53:06 -0700):
I have a multi-file upload form... Please looks at my code below. It's
in an initial stage. I'm trying to display the values of each text box
when I press "upload" but it doesn't seem to work... i don't know
what's the problem.. I'm using PHP5.


I can't figure out what this code is supposed to do, I'd bet that it
doesn't even output valid HTML:

<input type="file" name="image"1 /><br /><br />
<input type="file" name="image"2 /><br /><br />
<input type="file" name="image"3 /><br /><br />
<input type="file" name="image"4 /><br /><br />
<input type="file" name="image"5 /><br /><br />

--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Jul 17 '05 #2

P: n/a
On Wed, 22 Sep 2004 23:53:06 -0700, Ben wrote:
Hi everybody!
I have a multi-file upload form... Please looks at my code below. It's
in an initial stage. I'm trying to display the values of each text box
when I press "upload" but it doesn't seem to work... i don't know
what's the problem.. I'm using PHP5.

I haven't studied this in detail enough to know exactly where your problem
is, but I can see one problem right away. You have one misplaced quotation
mark, namely the one after image on this line:
<input type="file" name="image"<?php echo ifcond($i); ?> /><br /><br


I believe you want the return from function ifcond to be attached to the
word image, not come after it in the input tag.

But more importantly, you should try to rewrite your code so that it is
more readable. I very rarely mix clear html text with php tags for the
very reason that your piece of code here is very hard to debug. There are
many ways to write this piece of code, some more efficient than others,
but forgetting efficiency for the moment (php is fast anyway), this might
read better instead of the equivalent lines that you wrote:

<?php
for ($i=1; $i < 11; $i++)
{
$str = ifcond($i);
echo "Image$str:<input type=\"file\"
name = \"image$str\" /><br /><br />";
}
?>

Granted, it is clumsy having to use \" within to write a quote within the
echo command, but it reads better than having to parse a handful of php
tags by eyeball. Note here also, you have only one call to your function
instead of two. Also, if you are using a php-syntax aware editor,
something like Image$str looks very readable since the $str will be taken
as a PHP variable and made in a different color than Image.

Regards,
Steve, Denmark
Jul 17 '05 #3

P: n/a
As an afterthought to my reply, even better would be to use the following
construct, making your function ifcond unnecessary. If you get used to
the " if () ? true : false " syntax, things get a lot easier:

<?php
for ($i=1; $i < 11; $i++)
{
$str = ($i < 10) ? "0$i" : $i;
echo "Image$str:<input type=\"file\"
name = \"image$str\" /><br /><br />";
}
?>


Regards,
Steve, Denmark

Jul 17 '05 #4

P: n/a
Ben
coolsti <co*@goontrytospamme.com> wrote in message news:<pa****************************@goontrytospam me.com>...
As an afterthought to my reply, even better would be to use the following
construct, making your function ifcond unnecessary. If you get used to
the " if () ? true : false " syntax, things get a lot easier:

<snip>

thanx for tips steve..

i managed to find the problem... Have a look at the code below... The
reason "print_r($image);" doesn't show the selected items is
something to do with enctype="multipart/form-data". If I remove the
"enctype=....", "print_r($image);" displays the selected items.. But I
don't know the real relationship behind $_POST and enctype. Maybe
someone can explain me... I think the easier and a better way is to
use $_FILES with an array for "name" as in: <input type="file"
name="image[]">
<html>
<head>
</head>
<body>
<?php
for ($i=1; $i < 11; $i++) {
$str = ($i < 10) ? "0$i" : $i;
$image[$i - 1] = $_POST["image".$str];
}
print_r($image);
echo "<br />";
print_r($_FILES);

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
enctype="multipart/form-data" method="post">
<?php
for ($i=1; $i < 11; $i++) {
$str = ($i < 10) ? "0$i" : $i;
echo "Image$str:<input type=\"file\" name=\"image$str\" /><br
/><br/>";
} ?>
<input type="submit" value="Upload" /></p></form>
</form>
</body>
</html>

Ben
Jul 17 '05 #5

P: n/a
thanx for tips steve..


Have you read the PHP documentation (google search for "php documentation"
and take the first link) on file uploads? This explains pretty much how
things work, and yes, I believe when you do a file upload, your file names
are found in the $_FILES array. Note that your files are stored on your
server while the script is running with php-given temperary names in some
temperary directory, but you can access all this information using
$_FILES, see documentation. You need to do something with your uploaded
file before your upload script ends, e.g. move the temporary uploaded
files to more permanent locations, or they will disappear.

Regards,
Steve

Jul 17 '05 #6

P: n/a
> > I have a multi-file upload form... Please looks at my code below. It's
in an initial stage. I'm trying to display the values of each text box
when I press "upload" but it doesn't seem to work... i don't know
what's the problem.. I'm using PHP5.


I haven't studied this in detail enough to know exactly where your problem
is, but I can see one problem right away. You have one misplaced quotation
mark, namely the one after image on this line:
<input type="file" name="image"<?php echo ifcond($i); ?> /><br /><br


Sorry to reply to this message, but I'm unable to view the original message.

You can actually do this:
<input type = "file" name = "userfile[]" />
<input type = "file" name = "userfile[]" />
<input type = "file" name = "userfile[]" />
<input type = "file" name = "userfile[]" />
<input type = "file" name = "userfile[]" />

and in your PHP code do this:

foreach($_FILES['userfile']['name'] as $key=>$val) {
echo "NAME OF FILE ".$key.": ".htmlentities($val)."<br />\n";
echo "TMPNAME OF FILE ".$key.":
".htmlentities($_FILES['userfile']['tmp_name'][$key])."<br /><br />\n\n";
//do your validation and stuff
}

And that way you don't have to deal with missing files (like if there are 10
fields and the user selects files in 1,2,3,6,7 and 8.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.