where to display $_SESSION variable below
<p>I am trying to retrieve a name of the file uploaded and append it into .list. The problem is that the file name it is retrieving from the $_SESSION variable is always the last file updated and then it doesn't change.</p>

<p>For example if I upload these files below in the server:</p>

<pre><code>desert.jpg
tulips.jpg
lighthouse.jpg
desert_2.jpg
</code></pre>

<p>Then the file names it should append from $_SESSION should be:</p>

<pre><code>desert.jpg
tulips.jpg
lighthouse.jpg
desert_2.jpg
</code></pre>

<p>But instead it is appending this below for file names retrieved from $_SESSION on screen:</p>

<pre><code>desert.jpg
desert.jpg
desert.jpg
desert.jpg
</code></pre>

<p>How can it be fixed so that when a file is uploaded onto the server, it's file name is appended onto .list?</p>

<p>Below is the javascript function where $_SESSION variable is in the json code which displays the name of the file on the screen by appending it onto .list:</p>

<pre><code>  function stopImageUpload(success){

          var imagename = &lt;?php echo json_encode(isset($_SESSION ['fileImage']['name']) ? $_SESSION ['fileImage']['name'] : null); ?&gt;;
          if (success == 1){
             $('.listImage').append(imagename + '&lt;br/&gt;');
          }
          else {

            $('.listImage').append('No File');
          }

    return true;

    }
</code></pre>

<p>Below is the php code where it uploads the file:</p>

<pre><code>&lt;?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'] ) &amp;&amp; $_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'];

?&gt;

  &lt;script language="javascript" type="text/javascript"&gt;window.top.stopImageUpload(&lt;?php echo $result;?&gt;);&lt;/script&gt;
</code></pre>

<p>On seperate page I have a JSON which retrieves the $_SESSION:</p>

<pre><code>function stopImageUpload(success){

      var imagename = &lt;?php echo json_encode(isset($_SESSION ['fileImage']) ? $_SESSION ['fileImage'] : null); ?&gt;;
      if (success == 1){
         $('.listImage').append(imagename + '&lt;br/&gt;');
      }
      else {
        $('.listImage').append('&lt;br/&gt;');
      }

return true;

}
</code></pre>
