我有一个 $_SESSION 变量,它应该显示已上传文件的名称。问题是它没有这样做,它只显示 1 个文件和 1 个文件的文件名。例如,如果我上传文件“jellyfish.png”和“tulips.png”,它应该显示文件名“jellyfish.png”和“tulips.png”,$_SESSION['fileImage'];
但它会将两个文件名都显示为像这样的名字:“jellyfish.png”和“jellyfish.png”。
当我为上面的示例执行 a 时,无论上传哪个文件var_dump($_SESSION ['fileImage']);
,它都会输出:string(13) "Jellyfish.jpg"
如果我刷新浏览器并假设我上传到服务器的最后一个文件是“tulips.png”,那么它会string(10) "tulips.jpg"
为每个文件输出这个:,无论在下次刷新之前上传哪个文件。
有谁知道如何解决这个问题,以便在 $_SESSION 变量下显示每个上传文件的正确文件名?
下面是php脚本:
<?php
session_start();
$dirImage = "ImageFiles/";
if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$_SESSION ['fileImage'] = $_FILES ['fileImage']['name'];
$fileName = $_FILES ['fileImage'] ['name'];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
$filePrifix = basename ( $fileName, "." . $fileExt );
$i = 0;
while ( file_exists ( $fileDst ) ) {
$i ++;
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
}
// Move the file
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;
}
}
}
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
下面是包含 JSON 的 Javascript 代码:
function stopImageUpload(success){
var imagename = <?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?>;
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').append(imagename + '<br/>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
return true;
}
<?php
var_dump($_SESSION ['fileImage']);
?>