我有一个会话变量,它试图重新编写下面的所有图像文件名:
$_SESSION ['fileImage'] ['name'] = $_FILES ['fileImage'] ['name'];.
但问题是它只在屏幕上为所有上传的文件存储和显示1个文件名。例如,如果我上传了5个不同的文件,并且第一个上传的文件被称为'dog.png',那么对于所有文件,它将为屏幕上的所有文件显示文件名'dog.png'。 (在服务器上它正确显示每个文件的正确文件名,而不是在屏幕上显示。所以我的问题是,为什么它只存储和显示多个文件的一个文件名,如何让它显示所有文件屏幕上的文件名?
以下是json代码的行:
var imagename = <?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?>;
下面是上传文件的完整php脚本:
<?php
session_start();
$result = 0;
$errors = array ();
$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;
};