Jump to content

cant get this foreach loop to upload multiple files, it just dumps Array file...


mac007

Recommended Posts

Hello, all:

 

I'm trying to learn and do a simple foreach loop so I can upload multiple files at once, but all I actually get in umy "picts" directory is an "Array" file... maybe I got the $key variable in wrong places?

 

Thanks!

 

<code>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

 

 

<?php

if(isset($_FILES['picture']['tmp_name'])) {

$fileArray = $_FILES['picture']['tmp_name'];

foreach ($fileArray as $key) {

move_uploaded_file($key,"picts/" . $_FILES['picture']['name']);

}

}

?>

 

 

<form method="post" enctype="multipart/form-data">

  <label>

  <input type="file" name="picture[]" id="picture[]" />

  <br />

  <input type="file" name="picture[]" id="picture[]" />

  <br />

  <input type="file" name="picture[]" id="picture[]" />

  </label>

 

  <p>

    <input type="submit" name="button" id="button" value="Submit" />

  </p>

</form>

 

</body>

</html>

 

</code>

<?php
if(isset($_FILES['picture'])) {
$fileArray = $_FILES['picture'];
foreach ($fileArray as $key) {
move_uploaded_file($key['tmp_name'],"picts/" . $key['name']);
}
}
?>

try that. your files are uploaded as an array, so each of the $_FILES['picture'] need looping before trying to access the temp name etc

Like it says in your file. $_FILES['picture']['name'] is an array

$i=0;
foreach ($fileArray as $key) {
move_uploaded_file($key,"picts/" . $_FILES['picture']['name'][$i]);
$i++;
}

 

 

HTH

Teamatomic

well, thought I was done.. got one more question on this... how would I go about pulling the names of each file from the foreach loop so they can be written out to a database? do I need to unserialize them back out ?

Thank you Team...

 

I kind of get what you are saying, but thing is that this set of picts would belong to to 1 specific item/record... so I was trying to somehow get the each specific file name so that they in turn could be each inserted  within that record's filename data-fields. Since I have 3 form fields and corresponding 3 database 'imagee" fields, I kind of did this, which I think should work, as I am now able to isolate each name and insert it accordingly... I know, very primitive and backwards, but was only I way I was able to rig it... :)

 

<?php

 

if(isset($_FILES['picture']['tmp_name'])) {

$fileArray = $_FILES['picture']['tmp_name'];

$i=0;

foreach ($fileArray as $key) {

move_uploaded_file($key,"picts/" . $_FILES['picture']['name'][$i]);

$i++;

}

}

$fileName1 = $_FILES['picture']['name'][0];

$fileName2 = $_FILES['picture']['name'][1];

$fileName3 = $_FILES['picture']['name'][2];

 

echo $fileName1; // each of these variables I can now insert...

echo $fileName2;

echo $fileName3;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.