How to append the name of the file which has just been uploaded?
<p>I have a problem appending the imageNameArray where it is suppose to display file names which have been uploaded.</p>

<p>The problem is that lets say I previously uploaded a file (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 appending the name of the previous uploaded file (pig.png) and it does not append panda.png.</p>

<p>If I refresh page again and upload another file (tiger.png), then when I upload the file, it should display 'tiger.png'. But instead it is still just appending the name of the previously uploaded files (panda.png) and it does not append tiger.png.</p>

<p>If I upload another file (not refreshing page) such as monkey.png, then again it appends panda.png. No monkey.png. So it should of append tiger.png and monkey.png but instead it appends panda.png and panda.png.</p>

<p>All I want is that when a file is uploaded, it's name is appended. But how can this be achieved? Please show a coded example of how to fix it as I find it easier than an explanation saying why this is happening :)</p>

<p>Below is the javascript code where the appending occurs:</p>

<pre><code>    &lt;?php
    session_start();

    $idx = count($_SESSION ['fileImage'] - 1);
    $output = isset($_SESSION ['fileImage'][$idx]) ?
        $_SESSION ['fileImage'][$idx]['name'] : "";

    ?&gt;

&lt;script type="text/javascript"&gt;

 function imageClickHandler(imageuploadform){ 
      if(imageValidation(imageuploadform)){ 
          return startImageUpload(imageuploadform); 
      } 
      return false;
  }

function startImageUpload(imageuploadform){

              $(".imageCancel").click(function() {
              $('.upload_target').get(0).contentwindow
          return stopImageUpload();
    });

      return true;
}

    function stopImageUpload(success){
        var imageNameArray = new Array();
        imageNameArray = &lt;?php echo $output ?&gt;;
        var result = '';

        if (success == 1){
            result = '&lt;span class="msg"&gt;The file was uploaded successfully!&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;';

            for(var i=0;i&lt;imageNameArray.length;i++)
            {
                $('.listImage').append(imageNameArray[i]+ '&lt;br/&gt;');
            }
        }
        else {
            result = '&lt;span class="emsg"&gt;There was an error during file upload!&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;';
        }
        return true;
    }

&lt;/script&gt;

&lt;body&gt;

&lt;form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' &gt; 
Image File: &lt;input name='fileImage' type='file' class='fileImage' /&gt;&lt;/label&gt;&lt;br/&gt;&lt;label class='imagelbl'&gt;
&lt;input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /&gt;&lt;/label&gt;
&lt;/p&gt;&lt;ul class='listImage' align='left'&gt;&lt;/ul&gt;
&lt;iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'&gt;&lt;/iframe&gt;&lt;/form&gt;");



&lt;/body&gt;
</code></pre>

<p>Below is the php script (imageupload.php) where it uploads a file which is on another page from the javascript function above:</p>

<pre><code>&lt;?php
session_start();

$result = 0;
$errors = array ();
$dirImage = "ImageFiles/";

if (isset ( $_FILES ['fileImage'] ) &amp;&amp;
    $_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;
        }
    }
}

$_SESSION ['fileImage'][] = array('name' =&gt; $_FILES ['fileImage']['name']);
?&gt;
&lt;script language="javascript" type="text/javascript"&gt;window.top.stopImageUpload(&lt;?php echo $result;?&gt;);&lt;/script&gt;
</code></pre>
