上传多个文件时遇到问题。我有一个类别下拉和一个注释框,我需要将其与文件详细信息一起插入数据库。我在屏幕上显示了五个上传框。
如果我上传了一个文件,则注释为空白(即使它不是),并且类别始终设置为第一个值。经过一些测试,看起来好像最终的上传评论和类别框都覆盖了所有的过度。通过上传五个具有不同类别和评论的文件进行测试,所有文件都显示了最后的评论和类别。
我可以在脚本中看到问题,但我无法弄清楚如何为每个特定的上传实例分配类别或评论。
感谢任何帮助。
<?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;
}
?>
编辑:添加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>
答案 0 :(得分:1)
看看这是否有效。这适用于只有一个评论和类别的情况:
拿这个部分:
$comments = htmlentities( trim( $_POST['comments'] ) );
$category = htmlentities( trim( $_POST['category'] ) );
退出foreach循环:
if ( $files = convert_files( $_FILES["myTrainingFile"] ) ) {
foreach( $files as $i => $file ) {
由于您只从前一页获得评论和类别的一个值,因此在foreach循环中不需要这个值。此外,如果要为上载的每个文件添加注释和类别,则需要更改将值传递到此页面的上一页。如果您需要,则需要添加更多代码。
这些是OP添加更多代码后的更新:
改变这个:
<select name="category">
到此:
<select name="category[]">
和此:
<textarea class="GAH" name="comments"> </textarea>
到此:
<textarea class="GAH" name="comments[]"> </textarea>
然后在你原来的foreach循环中,改变这个:
$comments = htmlentities( trim( $_POST['comments'] ) );
$category = htmlentities( trim( $_POST['category'] ) );
到此:
$comments = htmlentities( trim( $_POST['comments'][$i] ) );
$category = htmlentities( trim( $_POST['category'][$i] ) );
这是基于以下假设:$ i是索引变量,当您遍历文件数组时,其值从0变为4。如果不是这种情况,那么你需要添加另一个索引变量,比如说$ count:
$count = 0;
foreach( $files as $i => $file ) {
// your other code goes here
$comments = htmlentities( trim( $_POST['comments'][$count] ) );
$category = htmlentities( trim( $_POST['category'][$count] ) );
$count ++;
}