Where to put the php code in the php script below
<p>I have a javascript variable below where it is suppose to retrieve  <code>$_SESSION['imagename']</code> from imageupload.php (a seperate page)</p>

<pre><code>var imagename = &lt;?php echo json_encode(isset($_SESSION['imagename']) ? $_SESSION['imagename'] : null); ?&gt;;
</code></pre>

<p>But what my question is where do I put this code below in the php script so that the session variable contains the name of the files and be able to be retrieve by the variable above?</p>

<pre><code>if (isset($_POST['fileImage'])) { // fileImage is the name of the file input
  $_SESSION['imagename'] =  $_FILES['fileImage']['name'];
}

$_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];
</code></pre>

<p>Below is the php script:</p>

<pre><code>$result = 0;

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {  

    $parts = explode(".",$_FILES['fileImage']['name']);
    $ext = array_pop($parts);
    $base = implode(".",$parts);
    $n = 2;

    while( file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
    $_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

    move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
    "ImageFiles/" . $_FILES["fileImage"]["name"]);
    $result = 1;

}
    else
      {
      move_uploaded_file($_FILES["fileImage"]["tmp_name"],
      "ImageFiles/" . $_FILES["fileImage"]["name"]);
      $result = 1;


      }

?&gt;

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