For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > September 2004 > multi-file upload - $_POST not working...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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

2004-09-23, 3:55 am

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
Alvaro G Vicario

2004-09-23, 3:55 am

*** 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
--
coolsti

2004-09-23, 8:55 am

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
coolsti

2004-09-23, 8:55 am

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

Ben

2004-09-24, 3:56 am

sti <coo@goontrytospamme.com> wrote in message news:<pan.2004.09.23.12.29.51.422000@goontrytospamme.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
cool

2004-09-24, 3:56 am


> 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

Shawn Wilson

2004-09-28, 4:00 pm

> > I have a multi-file upload form... Please looks at my code below. It's[color=darkred]
>
> 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:
>

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_na
me'][$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
shawn@glassgiant.com
http://www.glassgiant.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com