I have a problem appending the imageNameArray where it is suppose to display file names which have been uploaded.
The problem is that lets say I previously uploaded 3 files (cat.png, dog.png and pig.png), when I refresh page and upload another file (panda.png), then when I upload the file, it should display 'panda.png'. But instead it is just appending the name of the previously uploaded file (pig.png) and it does not append panda.png.
IT DISPLAYS:
pig.png
IT SHOULD BE:
panda.png
If I upload another file (not refreshing page) such as monkey.png, then again it appends pig.png. No monkey.png.
IT DISPLAYS:
pig.png
pig.png
IT SHOULD BE:
panda.png
monkey.png
How can the code below be fixed so that it appends the file names as it should do above?
Below is the javascript code where the appending occurs:
<?php
session_start();
$idx = count($_POST ['fileImage']) -1 ;
$output = isset($_POST ['fileImage'][$idx]) ? $_SESSION ['fileImage'][$idx]['name'] : "";
?>
function stopImageUpload(success){
var imageNameArray = ['<?php echo $output ?>'];
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
for(var i=0;i<imageNameArray.length;i++)
{
$('.listImage').append(imageNameArray[i]+ '<br/>');
}
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
return true;
}
Below is the php script where it uploads a file which is on another page from the javascript function above:
<?php
session_start();
$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";
if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
$fileName = $_FILES ['fileImage'] ['name'];
$fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExt = strtolower ( $fileExt );
$fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
if (count ( $errors ) == 0) {
if (move_uploaded_file ( $fileTemp, $fileDst )) {
$result = 1;
}
}
}
$_POST ['fileImage'][] = array('name' => $_FILES ['fileImage']['name']);
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
why you useing Session? try to add session_destroy(); in the end of the second script and the first one. I think its should delete all the previos data and then you know more whats the problem.