I have a problem when uploading multiple files. I have a category drop down and a comments box that I need to be inserted into the database along with the file details. I have five upload boxes displayed on the screen.
If I upload one file, the comment is blank (even when it shoudln't be) and the category is always set to the first value. Afte some testing it looks as though the final upload comment and category boxes are overwriting all of the overs. Tested by uploading five files with different categories and comments, all showed the last comment and category.
I can see the problem in the script but I can't figure out how to assign a category or comment to each particular instance of the upload.
Any help is appreciated.
<?php
$upload_dir = "training/trainingDocuments/";
$maxUploads = 5;
$msg = "Please select file(s) for uploading";
$errors = array();
if ( $files = convert_files( $_FILES["myTrainingFile"] ) ) {
foreach( $files as $i => $file ) {
$fileName = $file["name"];
$tempName = $file["tmp_name"];
$fileSize = $file["size"];
$fileExt = strtolower( pathinfo( $fileName, PATHINFO_EXTENSION ) );
$blacklist = array( 'exe','php','jsp','js','bat','asp','aspx','com','dmg' );
// $newPath = $upload_dir .$fileName;
$dateUploaded = time() . microtime();
if ( in_array( $fileExt, $blacklist ) ) {
$errors[$i] = "File type not allowed";
}
if ( !is_uploaded_file( $tempName ) ) {
//$errors[$i] = "Uploading ".$fileName." failed.";
}else{
echo '<h4>Uploading of :'.$fileName.' was a success.</h4>';
}
//if(file_exists($newPath)){
$name = pathinfo($fileName, PATHINFO_FILENAME);
$fileName = $name . '__' .uniqid(). '.' .$fileExt;
$newPath = $upload_dir .$fileName;
// }else{
//
// }
if ( isset( $errors[$i] ) ) {
continue;
}
if ( !move_uploaded_file( $tempName, $newPath ) ) {
//$errors[$i] = "Uploading ".$fileName." failed.";
}else{
//echo 'File moved';
}
$comments = htmlentities( trim( $_POST['comments'] ) );
$category = htmlentities( trim( $_POST['category'] ) );
if($file['name'] != ""){
$training->uploadDocument( $fileName, $category, $comments );
}
}
}
?>
<?php
function convert_files( $files ) {
if ( is_array( $files ) && !empty( $files["name"] ) ) {
if ( is_array( $files["name"] ) ) {
$merged = array();
foreach( $files["name"] as $i => $name ) {
$merged[] = array(
"name" => $name,
"type" => $files["type"][$i],
"size" => $files["size"][$i],
"error" => $files["error"][$i],
"tmp_name" => $files["tmp_name"][$i]
);
}
return $merged;
}
return array( $files );
}
return false;
}
?>
EDIT: ADDED HTML
<div id="uploadFormContainer">
<form id="uploadForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
<?php
$num = 0;
while($num < $maxUploads)
{?>
<div class="uploadFormE">
<label>File Category: </label>
<select name="category">
<option value="doc">Documents (pages, word, PDF etc)</option>
<option value="sheet">Spreadsheet</option>
<option value="vid">Video</option>
<option value="pres">Presentations</option>
<option value="img">Image</option>
<option value="quiz">Quiz</option>
<option value="other">Other</option>
</select>
</div>
<div class="uploadFormE">
<label>File Location: </label>
<input type="file" name="myTrainingFile[]" />
</div>
<div class="uploadFormE">
<label>Comments: </label>
<textarea class="GAH" name="comments"> </textarea>
</div>
<hr/>
<br />
<?php $num++;
}
?>
<input type="submit" value="Upload File(s)">
</form>
</div>