views:

43

answers:

3

Hey all,

I have a script I'm working out to upload up to 10 files at once. I have 10 seperate inputs in my html:

  <?php
    for($i=0;$i<10;++$i) {
         $num = $i+1;
         echo '
            <span>'.$num.'</span><input type="file" name="photo'.$i.'" />
              ';
     }
  ?>

And then I have my uploader which I have used before--in a slightly different form--but can't get to work now:

$path = "../path/to/folder/";
for($i = 0; $i < 20; $i++){
  $fileName = "photo". "$i";
  $filePath = "$path". "photo$i.jpg";

  if (!empty($_FILES['$fileName']['tmp_name'][$i])) {
       if (!copy($_FILES['$fileName']['tmp_name'][$i], $filePath)) {
            echo "failed to save '.$filePath.'";
        }
       chmod($filePath, 0644);
     }
 }

In a working version of this script I cut the for loop and just numbered each upload individually (I had this piece of code without the loop 10 times each with its own number). That worked great but it's really pretty ugly to look at a piece of code like that. Any ideas how to make this work?

UPDATED:

Still not having any luck I appreciate all the help. I think my problem lies in that the $_FILES arrays aren't being populated properly edits as follows:

changed HTML to:

  <?php
    for($i=0;$i<10;++$i) {
         $num = $i+1;
         echo '
            <span>'.$num.'</span><input type="file" name="photo[]" />
              ';
     }
  ?>

and the uploader script to:

$path = "../path/to/folder/";
for($i = 0; $i < 20; $i++){
  $fileName = "photo". "$i";
  $filePath = "$path$fileName.jpg";

  if (!empty($_FILES["photo"]["tmp_name"][$i])) {
       if (!copy($_FILES["photo"]["tmp_name"][$i], $filePath)) {
            echo "failed to save '.$filePath.'";
        }
       chmod($filePath, 0644);
     }
 }
+2  A: 

I believe (part of) your problem is the quoting. You are using single quotes in your loop which is causing the variables to not be evaluated. Try using the code below instead. There may be some other issues, but that should solve one aspect of your problem.

$path = "../path/to/folder/";
for($i = 0; $i < 20; $i++){
  $fileName = "photo". $i;
  $filePath = "$path". "$fileName.jpg";

  if (!empty($_FILES["$fileName"]['tmp_name'][$i])) {
       if (!copy($_FILES["$fileName"]['tmp_name'][$i], $filePath)) {
            echo "failed to save $filePath";
        }
       chmod($filePath, 0644);
     }
 }
jsuggs
+3  A: 

You are using $i twice:

$_FILES["$fileName"]["tmp_name"][$i]

the [$i] part makes no sense to me: You are already accessing each element through $fileName. I think you need to simply get rid of [$i].

Pekka
You should probably have mentioned you fixed his quoting error wrt. `'$fileName' -> "$fileName"` which... in the end might as well have been `$fileName`
Kristoffer S Hansen
@Kristoffer to be very honest, I didn't even notice that - I replaced it out of sheer habit :) Well spotted, cheers.
Pekka
@Pekka fixing errors without noticing them? That would make my workday so much easier.
Kristoffer S Hansen
thanks for the quick responses! but i've removed the i's and fixed the quotes but the photos still aren't uploading :-(
thomas
@thomas what does a `print_r($_FILES);` show?
Pekka
@pekka it's showing me an empty array -- literally "Array ()" :-/
thomas
@thomas thats... not so good
Kristoffer S Hansen
@kristoffer yeah i know it's not good
thomas
@thomas can you show your HTML form? The generated one in the "view source" view of your browser?
Pekka
@pekka no i can't because it's embarrassing. I opened the source and realized I forgot to include the enctype="multipart/form-data" in the <form> tag. Thanks so much for all the help though, at the very least i was able to optimize the script :-D
thomas
@thomas the enctype was in fact what I had in mind :) Good that it worked out.
Pekka
@pekka real glad i was able to figure this out, it's been driving me crazy since monday -- thanks for all the input
thomas
A: 

you have overcomplicated it
while nothing can be easier
just foreach over $_FILES

foreach ($_FILES as $file) {
  $filePath = $path. "photo".($i++).".jpg";
  if (!$file['error']) {
    move_uploaded_file($file['tmp_name'], $filePath));
  }
}

that's all for for the first naming convention.

Col. Shrapnel
not sure where exactly to implement this, I cut all the $_FILES stuff and dropped this code under for($i = 0; $i < 20; $i++) but still can't get anything to actually upload :-/ thanks
thomas
@thomas this is ALL code you need. Though if your $_FILES array is empty, it will obviously not work
Col. Shrapnel