My PHP code for file upload is not working.?

Ejiscool

New member
My PHP code for file upload is not working.?
I have been given some PHP code and some html to upload images to my website from my host. It does not seem to be working. Here it is:

<?php
//places files into same dir as form resides
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
move_uploaded_file(
$_FILES["pictures"]["tmp_name"][$key],
$_FILES["pictures"]["name"][$key]
) or die("Problems with upload");
}
}
echo "Your files, were uploaded succesfully";
echo "
";
echo "<a href='uploadform.php'>go back</a>";
?>

<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
Does the file the form is in need to be .PHP also?
 
Try this..
(not tested)

$files = extract_files($_FILES['pictures']);

foreach ($files as $f) {

if (is_uploaded_file($f["tmp_name"])) {

$path = "<image directory>/$f['name']";
//replace <image directory> with whatever the image directory is..
// for same directory as the form use:
//$path = $f['name'];

move_uploaded_file($f["tmp_name"], $path);

}
}

function extract_files($files) {
$extracted = array();
$z = 0;
foreach ($files as $key => $value) {
foreach ($value as $x) {
$extracted[$z++][$key] = $x;
}
}
return $extracted;
}

This works with arrays of images.. so you can have more than one file upload on your form.. e.g.

<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
etc
 
Back
Top