I have several functions that process a form that has multiple file uploads. Right now I'm doing everything long-handed.
Snippet 1: Calculate File Size of uploads
// Get the grand total file size of all uploaded files
$total_size = $_FILES['multi']['size'][0] + $_FILES['multi']['size'][1] + $_FILES['multi']['size'][2] + $_FILES['multi']['size'][3] + $_FILES['multi']['size'][4];
if($total_size <= 0){
$error_count++;
array_push($errors, "No file selected");
}elseif($total_size > 26214400){
$error_count++;
array_push($errors, "Total combined file size exceeds 25mb limit");
}
Snippet 2: Move the uploaded files (I have a code block for each file)
// Move File 1
if($_FILES['multi']['size'][0] > 0){
if(!move_uploaded_file($_FILES['multi']['tmp_name'][0], "/uploads/".$_FILES['multi']['name'][0]."")){
die("Could not move File 1");
}
}
I'm looking for a way of optimizing this so that if I decide to add more file upload inputs I'm not repeating addition functions or code blocks. I'm sure a foreach loop is the answer but I can't quite get it.