Helen Neely
Helen Neely

Reputation: 4740

Get name of Multiple File upload files

I have been trying to upload multiple files into a folder and save the names in a database table. Here's the code: I need to get the names of those uploaded files. I know my code is crap :(

Is there a way to loop through uploading those files and still return the file names? I need those names to be able to insert them into mysql table.

Thanks for your assistance.

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  Send these files:<br />
  <input name="pic1" type="file" /><br />
  <input name="pic2" type="file" /><br />
  <input name="pic3" type="file" /><br />
  <input name="pic4" type="file" /><br />
  <input type="submit" value="Send files" />
</form>

And the PHP script is below:

<?php

  $target = "uploads/"; 
  $target = $target . basename( $_FILES['pic1']['name']); 
  $target = $target . basename( $_FILES['pic2']['name']); 
  $target = $target . basename( $_FILES['pic3']['name']); 
  $target = $target . basename( $_FILES['pic4']['name']); 

 $pic1 =($_FILES['pic1']['name']); 
 $pic2 =($_FILES['pic2']['name']); 
 $pic3 =($_FILES['pic3']['name']); 
 $pic4 =($_FILES['pic4']['name']); 

$con = mysql_connect("localhost", "root", "");
  if (!$con){
    die('Could not connect: ' . mysql_error());
   }

   mysql_select_db("people", $con);

   $sql="INSERT INTO mtpupload (name, age, pic1, pic2, pic3, pic4 )
   VALUES('$_POST[name]','$_POST[age]','$pic1', '$pic2', '$pic3', '$pic4')";

  //---------Here, I want to insert all the pictures----------//    
    if(move_uploaded_file($_FILES['pic1']['tmp_name'], $target)) { 
       //do nothing
     }
     if(move_uploaded_file($_FILES['pic2']['tmp_name'], $target)) { 
       //do nothingelse{
     }if(move_uploaded_file($_FILES['pic3']['tmp_name'], $target)) { 
       //do nothing  echo "Sorry, the image was not moved from temp folder.";
     }if(move_uploaded_file($_FILES['pic4']['tmp_name'], $target)) { 
       //do nothing   
       echo "The was a problem uploading one of your images.";
     }
    //--------------Ends here---------------------//

    if (!mysql_query($sql,$con)){
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

   mysql_close($con)
   ?> 

UPDATE: turns out that this code is working but only saving one image in the folder. I think my move_uploaded_file is wrong. Any pointers?

Thanks again for your help.

Upvotes: 0

Views: 1450

Answers (3)

Helen Neely
Helen Neely

Reputation: 4740

Thanks folks for your suggestions, I finally got it working. I was using $target variable in many places. Renaming it to different variable helped.

Upvotes: 1

hakre
hakre

Reputation: 197767

Maybe this is helpful, using an array with the files field names and foreach:

$fields = array('pic1', 'pic2', 'pic3', 'pic4');
$fileNames = array();

foreach($fields as $field)
{
    $file = $_FILES[$field];
    # you can now process each file on it's own.

    $fileNames[$field] = $file['name']; # store all names into an array
    ...
}

Upvotes: 1

Jan-Henk
Jan-Henk

Reputation: 4874

What about this:

foreach ($_FILES as $file) {
    echo $file['name']; // File name of file on user's computer
    echo $file['tmp_name']; // Full path to the file on the server
}

Upvotes: 0

Related Questions