Jump to content

[SOLVED] multiple file upload move_uploaded_file help


dennismonsewicz

Recommended Posts

I am doing a multiple file upload and I was wondering how would the array breakdown for the $_FILES var as far as using the ['tmp_name'] goes?

 

This is the code I am working with:

 

foreach($_FILES['uploadedfile']['name'] as $key) {
	if(!empty($key)) {												
	if(move_uploaded_file("uploads/" . $_FILES['uploadedfile']['tmp_name'], $target_path)) {											
	$att_qry = mysql_query("INSERT INTO uploads (upload_name, upload_path, project_name, project_id) VALUES ('$key', '$target_path', '$project_name', '" . $idresults->id . "')")or die(mysql_error());
	}
}											
} 

 

How would does the ['tmp_name'] and the $key values match up when moving the file?

if i remember right, it should be like so:

 

foreach($_FILES['uploadedfile']['name'] as $n=>$name) {
  if(move_uploaded_file("uploads/" . $_FILES['uploadedfile']['tmp_name'][$n], $target_path))
  {
    $att_qry = mysql_query("INSERT INTO uploads (upload_name, upload_path, project_name, project_id) VALUES ('$name', '$target_path', '$project_name', '" . $idresults->id . "')")or die(mysql_error());
  }
}

 

...but that will keep overwritting $targetpath...i assume you want a different $targetpath for each file?

this is what i have:

 

foreach($_FILES['uploadedfile']['name'] as $key) {
if(!empty($key)) {
$target_path = "uploads/" . $key . "<br />";
echo $target_path;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$key], $target_path)) {											
$att_qry = mysql_query("INSERT INTO uploads (upload_name, upload_path, project_name, project_id) VALUES ('$key', '$target_path', '$project_name', '" . $idresults->id . "')")or die(mysql_error());
} else {
echo '<p>Don\'t work boss!</p>';
}
}											
} 

 

And no each file needs to go in the same uploads folder

try this:

 

foreach($_FILES['uploadedfile']['name'] as $n => $name) {
  if(!empty($name)) {
    $target_path = "uploads/" . $name;
    echo $target_path . "<br />";
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$n], $target_path)) {
      mysql_query("INSERT INTO uploads (upload_name, upload_path, project_name, project_id) VALUES ('$name', '$target_path', '$project_name', '" . $idresults->id . "')")
        or die(mysql_error());
    } else {
      echo '<p>Didn\'t work boss!</p>';
    }
  }
}

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.