我有一个生成多个单个文件上传的表单。它们目前上传正常,但上传文件会在数组中覆盖$ _SESSION ['file'] [0]。
<?php
if(isset($_SESSION['UploadCount'])){
for($i=0;$i<=$_SESSION['UploadCount'];$i++){ ?>
<div class="form-group col-md-6">
<span class="control-label">Upload:</span>
<div class="form-control file-input">
<label for="Attachment">
<span class="btn btn-primary">
<input type="file" name="Attachment<?php echo $i; ?>" value="<?php echo $i; ?>"id="Attachment" onchange="this.form.submit();"/>
Choose File
</span>
</label>
<input class="file-name" type="text" readonly="readonly" placeholder="<?php if(isset($_SESSION['file'][$i])){ echo $_SESSION['file'][$i]; } else { echo "No file selected"; } ?>" />
</div><br>
<?php if(isset($_SESSION['UploadError'][$i])){ ?><span class="error"><?php echo $_SESSION['UploadError'][$i]; ?></span><?php } ?>
</div>
<div class="form-group col-md-12">
<label for="Topic" class="control-label">Topic Description</label>
<textarea class="form-control" name="Topic" cols="20" rows="3"><?php if(isset($_SESSION['Topic'])){ echo $_SESSION['Topic']; } ?></textarea>
</div><?php } } ?>
要上传的php是:
for($i=0;$i<=$_SESSION['UploadCount'];$i++){
if(isset($_FILES["Attachment".$i]["name"])){
if($_FILES["Attachment".$i]["name"])
{
$uperror = "";
include ($_SERVER['DOCUMENT_ROOT'].'/Core/MultiUpload.php');
if($uperror==""){
$_SESSION['file'][$i] = basename($_FILES["Attachment".$i]["name"]);
$_SESSION['stored'][$i] = $target_file;
} else {
$_SESSION['UploadError'][$i] = $uperror;
}
header( "Location: ".$hostname."/consultation/?page=".$Page."&assign=".$Assign ) ;
exit;
}
}
}
MultiUpload.php文件是:
<?php
$uploadfoldercheck = "../Uploads/".$_SESSION['CompanyName']."/";
$uploadfoldercheck = str_replace(' ', '_', $uploadfoldercheck);
if (!file_exists($uploadfoldercheck))
{
mkdir($uploadfoldercheck, 0755, true);
$copy_index = $uploadfoldercheck."/index.php";
copy('../Uploads/index.php', $copy_index);
}
$filename = "../Uploads/".$_SESSION['CompanyName']."/".$_SESSION['UserID']."/";
$filename = str_replace(' ', '_', $filename);
if (!file_exists($filename))
{
mkdir($filename, 0755, true);
$copy_index = $filename."/index.php";
copy('../Uploads/index.php', $copy_index);
}
$target_file = $filename . basename($_FILES["Attachment".$i]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
$uperror = "Sorry, file already exists:".$target_file;
$uploadOk = 0;
}
// Check file size
if ($_FILES["Attachment"]["size"] > 2000000) {
$uperror = "File to large. Max 2MB.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "pdf" ) {
$uperror = "Sorry, only PDF files are allowed.";
$uploadOk = 0;
}
if($uploadOk=="1")
{
if (move_uploaded_file($_FILES["Attachment".$i]["tmp_name"], $target_file)) {
} else {
$uperror = "Unable to load File!";
}
}
?>
上传计数已初始化:
if(!isset($_SESSION['UploadCount'])){
$_SESSION['UploadCount']=3;
}