Jump to content

file upload script question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
Xhris

Xhris

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
I have an upload script that I use for uploading files on my website. Now I am wanting to make it so that I can have multiple uploads at once. Here is the code I am using .

<form action="sports_upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file"><b><font color="#CCCCCC">File:</font></b> </label>
<input type="file" name="file" id="file" /> <br />
<label for="file"><b><font color="#CCCCCC">File:</font></b> </label>
<input type="file" name="file" id="file" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
</form>

<?php

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 9000000))
  {
  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("sports/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "sports/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "sports/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>


With the above I have it showing 2 upload boxes but when I choose two files only one gets uploaded. What did I miss here?

Thanks

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
In your HTML you need to add [] to "file" so that it is pased as an array. Here is how your HTML should look:

<form action="sports_upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file"><b><font color="#CCCCCC">File:</font></b> </label>
<input type="file" name="file[]" id="file" /> <br />
<label for="file"><b><font color="#CCCCCC">File:</font></b> </label>
<input type="file" name="file[]" id="file" />
<br /><br />
<input type="submit" name="submit" value="Upload" />
</form>  

In your PHP you can then loop through the file array:

foreach ($_FILES["file"] as $file) {
   echo $file["name"];
   // more logic
}


#3
Xhris

Xhris

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
Where do I add the second part? Anywhere specific?

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Not really, it was just an example to show you how to loop through the files array.

#5
Xhris

Xhris

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
Ah.. Ok I thought I had to put that some where to make it work, No wonder I got errors.LOL

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
No, lol. Check here: PHP: Handling file uploads - Manual

Take a look at example #3 and the user comments for examples.

#7
Xhris

Xhris

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
I got what I need thanks.

#8
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Great! Mind if we see your completed script?

#9
Xhris

Xhris

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
Hi Jordan. What I did was google for tutorials on what I was wanting for the upload script. I found one that worked good enough for my needs. But if you want to see the script I can still show what I ended up with. It was in 3 different files.

#10
Xhris

Xhris

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
Well I ended up doing a diferent one and here is what I used:

<form action="sports_upload.php" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br />
<input type="file" name="pictures[]" /><br /><br />
<input type="submit" value="Upload" />
</p>
</form>	

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "./sports/$name");
    }
}
?>

This seems to work like a charm so far.

#11
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
With your original script, it could be cut down a fair bit as Jordan mentioned with the array and then loop through it.

Also are you going to be using this script on a website or not? Also I believe there could be a way through JavaScript that when you click on a button another input field could appear.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yep, this is possible. Take, for example, the Gmail interface, where you can click a button to instantly add a new email input field to the list.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums