Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

Multiple upload fields

Hi all,

I am looking at the following code:

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html> 

Open in new window


and the php

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

Open in new window



My question is...How can I get it to work with multiple upload fields..

For example:

<input type="file" name="file" id="file" />
<input type="file" name="file2" id="file2" />
<input type="file" name="file3" id="file3" />

Thanks

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Avatar of error77
error77

ASKER

I would prefar not to have to use a different script as the above is already working ... I just need to be about to have multiple imput files
then i guess you just have to repeat php codes for all file tags, may be you can refactor the method to pass the name of the file field, since you are using name 'file' to get the file object
Avatar of error77

ASKER

Why won't this work on my code:

<input type="file" name="file" id="file" />
<input type="file" name="file2" id="file2" />
<input type="file" name="file3" id="file3" />

?
because you are only checking the file input with name 'file', you need to execute that code for 'file2' and 'file3' also
Avatar of error77

ASKER

OK, I see it now so if there a way to modify the code so it accepts file1, file2 etc without having to add the same code many times?
See this post #36483333 above
Do you know how to refactor the code into a separate method?
Avatar of error77

ASKER

Hi gurvinder372,

No, I don't know how to do that to my code. could you help?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error77

ASKER

Hi Ray,

It's looks very commented and easy to understand so thanks for that.

I need to add this code to an existing project so my question is ... can't I break this code into 2 parts:

1. Form post

2. The upload code

So, that I can have all the form submit code in the same external file and just have the form parts merged with my current project?

Do you know what I mean?

Sure, I think that is possible.  One script would have the form (the HTML) and it would post to the upload script (the PHP).  Professionally, I like to combine these, but it is quite possible to separate them.
Avatar of error77

ASKER

Would the script work if I leave:

<form name="UploadForm" enctype="multipart/form-data" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
    <p>
    Find the file(s) you want to upload and click the "Upload" button below.
    </p>

Open in new window


in the main page and move the rest of the code into another page ... then add to the form: action="theExternalPage.php"  ?
It's almost always impossible to answer questions like Would the script work if... because the original script I posted has 183 lines, and the hypothetical script has 5 lines.  Could it be made to work?  Yes, of course, but there would be a great deal more code involved.  Would I do it?  No.

In my professional opinion, nothing is gained, and organization is lost, when a programmer separates the form script from the action script. The two scripts are designed to work together, and separating them just introduces more potential points of failure.  Example of lost organization: Everything up through line 44 is common to the two scripts.  If you separate the scripts, you now will need to replicate these "initialization" statements, and this means you will need to keep track of the code in two separate places.  Professionals would not want to add the risk of confusion that unnecessary complexity might bring.

Anyway, best of luck with it, ~Ray
Avatar of error77

ASKER

My problem is that I posted some code hoping that it could be modified to my needs and I then get a new script which I need to adapt to my already existing project.

The problem is that I cannnot just replace the code ... I need to adapt it too, so that's why I ask the questions about modifing the script.

You script could be the greatest but I need have to break it.

I'll give it a try and get back.

Thanks
...I posted some code hoping that it could be modified to my needs...

Sigh.  I understand.  We see a lot of questions here at EE that start with, "How can I make this code do [something different]" and when we provide a teaching example we often get a response that goes something like, "but I want to use my code."  That is why we post the teaching examples, because the posted code does not work, and the teaching examples provide guidance about how to modify the code in such a way that it can be made to work.  We cannot write your code for you -- that is what a paid professional programmer does for a living.  We are just volunteers here who try to help and educate to the best of our ability, given an environment that allows for a written back-and-forth dialog.

If there is anything in the example that you do not understand, or any principles that are unclear, please post back and I will try to help.