I have a $_SESSION variable which should display names of files which have been uploaded. The problem is that it is not doing this, it is only display the name of a file for 1 file and 1 file only. For example if I upload files 'jellyfish.png' and 'tulips.png', it should display the file names 'jellyfish.png' and 'tulips.png' for $_SESSION['fileImage'];
but instead it is displaying both file names as the first name like this: 'jellyfish.png' and 'jellyfish.png'.
When I do a var_dump($_SESSION ['fileImage']);
for the example above, it outputs this: string(13) "Jellyfish.jpg"
, no matter which file is uploaded. If I refresh the browser and lets say the last file I uploaded into the server was 'tulips.png', then it outputs this: string(10) "tulips.jpg"
, for every file no matter which file is uploaded before the next refresh.
Does anyone know how to fix this so it displays the correct file names for each uploaded file under the $_SESSION variable?
Below is the php script:
<?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>
Below is the Javascript code which contains the JSON:
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']);
?>