Multiple file uploads

This is a discussion on "Multiple file uploads" within the PHP Forum section. This forum, and the thread "Multiple file uploads are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 5th, 2007, 22:05
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Multiple file uploads

Hi i am trying to set up a mutliple file upload PHP form but cannot get it to work for more than one upload. My code is as follows:


FORM CODE:
PHP: Select all

<FORM ACTION="tender_request.phpMETHOD="post" ENCTYPE="multipart/form-data" NAME="form1">
    <
INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000000" />

<
TABLE WIDTH="737" ALIGN="CENTER" CELLPADDING="5" CLASS="contact_text">
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Company Name:&nbsp;*</DIV>
</
TD>
<
TD WIDTH="69%" VALIGN="top">
<
INPUT NAME="company_name" ID="company_name" TYPE="text" SIZE="30" MAXLENGTH="50" TABINDEX="10">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Email Address :&nbsp;*</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="email" ID="email" TYPE="text" SIZE="30" MAXLENGTH="50" TABINDEX="11">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Telephone Number:&nbsp;*</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="telephone" ID="telephone" TYPE="text" SIZE="30" MAXLENGTH="50" TABINDEX="12">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Project Title:&nbsp;* </DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="project_title" TYPE="text" ID="project_title" SIZE="30" MAXLENGTH="50" TABINDEX="13">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 1:&nbsp;*</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 2:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 3:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 4:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 5:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 6:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 7:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 8:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">&nbsp;</TD>
<
TD VALIGN="top"The following file formats are allowed to be uploaded to us: <STRONG>JPEG</STRONG>, <STRONG>DOC</STRONG>, <STRONG>PDF</STRONG> </TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Message:&nbsp;</DIV>
</
TD>
<
TD VALIGN="top">
<
TEXTAREA NAME="message" COLS="50" ROWS="6" CLASS="contactForm" ID="message"  TABINDEX="15"></TEXTAREA>
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">&nbsp;</TD>
<
TD VALIGN="top">
<
INPUT NAME="Send" TYPE="submit" CLASS="btn" VALUE="Submit Request" TABINDEX="16"/>
</
TD>
</
TR>
</
TABLE>

</
FORM
PHP CODE:
PHP: Select all

  <?php
//import form information
  
$company $_REQUEST['company_name'] ;
  
$subject $_REQUEST['project_title'] ;
  
$telephone $_REQUEST['telephone'] ;
        
$message $_REQUEST['message'] ;
        
$email $_REQUEST['email'] ;
        
$uploaddir 'uploads/';

        
$uploadfile $uploaddir basename($HTTP_POST_FILES['userfile']['name']);
     
$uploadfile2 $uploaddir basename($HTTP_POST_FILES['userfile2']['name']);
        
$uploadfile3 $uploaddir basename($HTTP_POST_FILES['userfile3']['name']);
        
$uploadfile4 $uploaddir basename($HTTP_POST_FILES['userfile4']['name']);
        
$uploadfile5 $uploaddir basename($HTTP_POST_FILES['userfile5']['name']);
        
$uploadfile6 $uploaddir basename($HTTP_POST_FILES['userfile6']['name']);
        
$uploadfile7 $uploaddir basename($HTTP_POST_FILES['userfile7']['name']);
        
$uploadfile8 $uploaddir basename($HTTP_POST_FILES['userfile8']['name']);
                        


/*
Simple form validation
check to see if an email and message were entered
*/
//if no message entered and no email entered print an error  
if (empty($company)){
  print 
"You did not enter a <STRONG>Company Name</STRONG> in the Tender Request form. <br> Please <a href=tenders.html>Click here</a> to try again.";
  }
   
//if no message entered send print an error  
        
elseif (empty($telephone)){
  print 
"You did not enter a <STRONG>Telephone Number</STRONG> in the Tender Request form. <br> Please <a href=tenders.html>Click here</a> to try again.";
  }
    elseif (empty(
$subject)){
  print 
"You did not enter a <STRONG>Project Title</STRONG> in the Tender Request form. <br> Please <a href=tenders.html>Click here</a> to try again.";
  }
  
//if no email entered send print an error  
  
elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)){
  print 
"The email address entered is not valid. .<br><a class=sub-links href=contact.php>Click here</a> to try again.<br>";
  }


 

  
//if the form has both an email and a message
  
else {
        

// print a response message


//Thank the user by name if they entered a name
if (!empty($name)) {
  print 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>";
}

$body"Company Name: " $company"\n\nTelephone: " $telephone.  "\n\nEmail: " $email"\n\nProject Title: " $subject;

//mail the form contents
mail("robukni@gmail.com""Tender Request"$body"From: $email" );


if (
move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";
                } 
elseif (
move_uploaded_file($HTTP_POST_FILES['userfile2']['tmp_name'], $uploadfile2)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile3']['tmp_name'], $uploadfile3)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile4']['tmp_name'], $uploadfile4)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile5']['tmp_name'], $uploadfile5)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile6']['tmp_name'], $uploadfile6)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile7']['tmp_name'], $uploadfile7)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile8']['tmp_name'], $uploadfile8)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";
}             

else {
    echo 
"No file was selected to upload!\n";
}


 
  }

?>
Any help much appreciated.

Thanks
Robert
Reply With Quote

  #2 (permalink)  
Old Sep 5th, 2007, 22:34
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Multiple file uploads

you have to name the file inputs differently
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #3 (permalink)  
Old Sep 5th, 2007, 22:36
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multiple file uploads

Sorry i have tried that but it didn't work so i reverted back to that one.

Any other suggestions?


Thanks
Robert
Reply With Quote
  #4 (permalink)  
Old Sep 6th, 2007, 08:41
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multiple file uploads

There are two reasons why it's not working.

alexgeek is right, in your html you have to change the name attribute to:

userfile
userfile2
userfile3...

on the php side of things, you are saying, in english:
if the first file is saved, then don't worry about the other files

you're almost there, you just need to replace all the "elseif's" in the code below with with "if".

Also while you're at it, you should use $_FILES intstead of $HTTP_POST_FILES as it is depreceated.

PHP: Select all

if (move_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'], $uploadfile)) {

    echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";
                } 
elseif (
move_uploaded_file($HTTP_POST_FILES['userfile2']['tmp_name'], $uploadfile2)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile3']['tmp_name'], $uploadfile3)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile4']['tmp_name'], $uploadfile4)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile5']['tmp_name'], $uploadfile5)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile6']['tmp_name'], $uploadfile6)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile7']['tmp_name'], $uploadfile7)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";

elseif (
move_uploaded_file($HTTP_POST_FILES['userfile8']['tmp_name'], $uploadfile8)) {
echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File that you selected is valid, and was successfully sent to us.\n";


Last edited by jimz; Sep 6th, 2007 at 08:48.
Reply With Quote
  #5 (permalink)  
Old Sep 6th, 2007, 09:30
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multiple file uploads

You could probably change your code to be quite a bit more graceful and cut back on your duplication.

Changing the names of the file inputs to

<input type="file" name="userfile[]" tabindex="26" size="16" />

and adding an input like

<input type="hidden" name="max_uploads_allowed" value="8" />

Changing the value to the amount of upload inputs on your page. Changing the name of the files in this manner will mean PHP will create an array in the superglobal $HTTP_POST_FILES['username'] or $_FILES

With an enumerated array that you can cycle through like

PHP: Select all



 
for ($i0$i $_REQUEST['max_allowed_uploads']; $i++)
{
   
$location $uploaddir basename($_FILES['userfile'][$i]['name']);

     if (
move_uploaded_file($_FILE[$i], $uploaddir))
     echo 
"Thankyou for your Tender Request at H.McLarnon and Son. <br><br>The File ({$_FILE['userfile'][$i]['name']} )that you selected is valid, and was successfully sent to us.\n"


Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #6 (permalink)  
Old Sep 6th, 2007, 11:27
Up'n'Coming Member
Join Date: Jan 2006
Location: NI
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multiple file uploads

I have tried that but when i click submit it just loads a blank page.

PHP: Select all

<FORM ACTION="tender_request.phpMETHOD="post" ENCTYPE="multipart/form-data" NAME="form1">
    <
INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000000" />
                <
input type="hidden" name="max_uploads_allowed" value="8" />
<
TABLE WIDTH="737" ALIGN="CENTER" CELLPADDING="5" CLASS="contact_text">
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Company Name:&nbsp;*</DIV>
</
TD>
<
TD WIDTH="69%" VALIGN="top">
<
INPUT NAME="company_name" ID="company_name" TYPE="text" SIZE="30" MAXLENGTH="50" TABINDEX="10">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Email Address :&nbsp;*</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="email" ID="email" TYPE="text" SIZE="30" MAXLENGTH="50" TABINDEX="11">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Telephone Number:&nbsp;*</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="telephone" ID="telephone" TYPE="text" SIZE="30" MAXLENGTH="50" TABINDEX="12">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Project Title:&nbsp;* </DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="project_title" TYPE="text" ID="project_title" SIZE="30" MAXLENGTH="50" TABINDEX="13">
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 1:&nbsp;*</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 2:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 3:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 4:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 5:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 6:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 7:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile[]" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">
<
DIV ALIGN="RIGHT">Upload tender Request 8:</DIV>
</
TD>
<
TD VALIGN="top">
<
INPUT NAME="userfile" TYPE="file" TABINDEX="26" SIZE="16">
</
TD>
</
TR>
<
TR>
<
TD VALIGN="top">&nbsp;</TD>
<
TD VALIGN="top"The following file formats are allowed to be uploaded to us: <STRONG>JPEG</STRONG>, <STRONG>DOC</STRONG>, <STRONG>PDF</STRONG> </TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">
<
DIV ALIGN="RIGHT">Message:&nbsp;</DIV>
</
TD>
<
TD VALIGN="top">
<
TEXTAREA NAME="message" COLS="50" ROWS="6" CLASS="contactForm" ID="message"  TABINDEX="15"></TEXTAREA>
</
TD>
</
TR>
<
TR>
<
TD WIDTH="31%" VALIGN="top">&nbsp;</TD>
<
TD VALIGN="top">
<
INPUT NAME="Send" TYPE="submit" CLASS="btn" VALUE="Submit Request" TABINDEX="16"/>
</
TD>
</
TR>
</
TABLE>

</
FORM
PHP: Select all

  &