user1759682
user1759682

Reputation: 257

Uploading files with PHP

I am using a form for users to upload files to my website. I want to allow them to upload multiple photos at once, so I am using the HTML5 "multiple" attribute.

My HTML:

<form method="post" action="save.php">
    <input type="file" name="uploads[]" multiple="multiple" />
    <input type="submit" name="submit" value="submit"/>
</form>

save.php:

<?php
  foreach ($_FILES['uploads']['name'] as $file) {
    echo $file . "<br/>";
    $file= time() . $_FILES['uploads']['name'];
    $target= UPLOADPATH . $file;
    move_uploaded_file($_FILES['uploads']['tmp_name'], $target)
    or die('error with query 2');
   }

But, for some reason when I run the script, I get an error saying undefined index: uploads. And an error saying that I have an invalid argument supplied for foreach(). What could I be dong wrong?

Thanks

UPDATE

Okay, setting the enctype="mulitpart/form-data" worked. Now, I am having trouble with moving the file. I am getting the error move_uploaded_file() expects parameter 1 to be string, array given. What am I doing wrong here?

Thanks again

Upvotes: 4

Views: 1633

Answers (4)

antelove
antelove

Reputation: 3348

index.html

<form method="post" action="save.php" enctype="multipart/form-data">
    <input type="file" name="uploads[]" multiple="multiple" />
    <input type="submit" name="submit" value="Upload Image"/>
</form>

save.php

<?php    
  $file_dir  = "uploads";    
  if (isset($_POST["submit"])) {

    for ($x = 0; $x < count($_FILES['uploads']['name']); $x++) {                

      $file_name   = time() . $_FILES['uploads']['name'][$x];
      $file_tmp    = $_FILES['uploads']['tmp_name'][$x];

      /* location file save */
      $file_target = $file_dir . DIRECTORY_SEPARATOR . $file_name;

      if (move_uploaded_file($file_tmp, $file_target)) {                        
        echo "{$file_name} has been uploaded. <br />";                      
      } else {                      
        echo "Sorry, there was an error uploading {$file_name}.";   
      }                 

    }               
  }    
?>

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227190

In order to upload files in the first place, you need enctype="multipart/form-data" on your <form> tag.

But, when you upload multiple files, every key in $_FILES['uploads'] is an array (just like $_FILES['uploads']['name']).

You need to get the array key when looping, so you can process each file. See the docs for move_uploaded_file for more deatils.

<?php
  foreach ($_FILES['uploads']['name'] as $key=>$file) {
    echo $file."<br/>";
    $file = time().$file;
    $target = UPLOADPATH.$file;
    move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target)
    or die('error with query 2');
}

Upvotes: 0

RDK
RDK

Reputation: 4560

try this html code: <form method="post" action="save.php" enctype="multipart/form-data"> Then in PHP:

if(isset($_FILES['uploads'])){
foreach ($_FILES['uploads']['name'] as $file) {
    echo $file . "<br/>";
    $file= time() . $_FILES['uploads']['name'];
    $target= UPLOADPATH . $file;
    move_uploaded_file($_FILES['uploads']['tmp_name'], $target)
    or die('error with query 2');
   }
} else {
   echo 'some error message!';
}

Upvotes: 0

Alexey Sidorov
Alexey Sidorov

Reputation: 866

You need the proper enctype to be able to upload files.

<form method="post" enctype="multipart/form-data" action="save.php">

Upvotes: 5

Related Questions