我试图找回一个文件上载的名字,并将其输入名单上。 问题是,从“SESSION”变量中检索的档案名称始终是最新档案,然后是没有变化的。
例如,如果我把这些档案上载到服务器以下的话:
desert.jpg
tulips.jpg
lighthouse.jpg
desert_2.jpg
然后,它应从_SESSION美元中填写的档案名称应为:
desert.jpg
tulips.jpg
lighthouse.jpg
desert_2.jpg
但是,在从屏幕上的“SESSION”美元中检索的文档名称方面,下文对此作了评估:
desert.jpg
desert.jpg
desert.jpg
desert.jpg
如何确定,以便在档案上上载到服务器时,其档案名称被附在名单上?
下面是“javascript”功能,即“SESSION”变量在“json”代码中,显示屏幕上文档的名称,将其贴上:
function stopImageUpload(success){
var imagename = <?php echo json_encode(isset($_SESSION [ fileImage ][ name ]) ? $_SESSION [ fileImage ][ name ] : null); ?>;
if (success == 1){
$( .listImage ).append(imagename + <br/> );
}
else {
$( .listImage ).append( No File );
}
return true;
}
The Below is the php Code where it uploads the file:
<?php
session_start();
$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";
$allowedTypes = array (
"image/jpeg",
"image/gif",
"image/pjpeg",
"image/jpg",
"image/png",
"image/tif"
);
if (isset ( $_FILES [ fileImage ] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES [ fileImage ] [ name ];
$fileSize = $_FILES [ fileImage ] [ size ];
$fileTemp = $_FILES [ fileImage ] [ tmp_name ];
$fileType = $_FILES [ fileImage ] [ type ];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
if (empty ( $fileName ))
continue;
if (! in_array ( $fileType, $allowedTypes )) {
$errors [] = "File Type is not accepted";
}
if(!is_writable($dirImage ))
{
$errors [] = "File Destination not writeable";
}
$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;
}
}
}
$_SESSION [ fileImage ][] = $_FILES [ fileImage ][ name ];
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
在适当网页上,有1名初等干事,可检索到_SESSION:
function stopImageUpload(success){
var imagename = <?php echo json_encode(isset($_SESSION [ fileImage ]) ? $_SESSION [ fileImage ] : null); ?>;
if (success == 1){
$( .listImage ).append(imagename + <br/> );
}
else {
$( .listImage ).append( <br/> );
}
return true;
}