How to retrieve the name of the file's name from the server using javascript?
<p>I have a function below where when the file stops uploading, it displays a message whether upload was successful or not and then more importantly displays the name of the file uploaded using this code below:</p>

<pre><code> $('.listImage').append(nameimagefile + '&lt;br/&gt;');
</code></pre>

<p>This code above retrieves the name of the file selected from the file input. The problem with this though is that I don't want the name of the file entered in the file input. Instead I want the name of the file which is uploaded into the server.</p>

<p>For example if I have uploaded 2 files which are both tulips.png, in the server they are saved and uploaded as tulips.png and tulips2.png but because I am using <code>$('.listImage').append(nameimagefile + '&lt;br/&gt;');</code>, it displays both file names as 'tulips.png' which is not what I want.</p>

<p>So this is where I decided i want  to retireve the name of the file not from the value of the input file but the name saved when uploaded in the file.</p>

<p>How can I code this in the javascript function? </p>

<p>The javascript function and php script are on seperate pages but I do use a call back function in the php script to the javascript function:</p>

<p>Below is the form code:</p>

<pre><code>&lt;form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='stopImageUpload(this);' class='imageuploadform' &gt;
    &lt;p&gt;Image File: &lt;input name='fileImage' type='file' class='fileImage' /&gt;
    &lt;input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /&gt;
    &lt;/p&gt; 
    &lt;ul class='list'&gt;&lt;/ul&gt;
    &lt;/form&gt;
</code></pre>

<p>Below is the javascript function which gets the name of the file and displays a message if file is successful or not:</p>

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

          var nameimagefile = $('.fileImage').val();
          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;';
             $('.listImage').append(nameimagefile + '&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;

}
</code></pre>

<p>Finally below is the php script with the javascript call back function. aLl it does is upload file if file exists or not in server's folder. If it exists then it gives it a different file name by adding a name at end of file name, if it doesn't exist then it uploads file with same name:</p>

<pre><code>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.window.stopImageUpload(&lt;?php echo $result;?&gt;);&lt;/script&gt;
</code></pre>

<p>Hopefully that is all the info you need and thank you.</p>
