Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:26+00:00 2026-06-03T03:11:26+00:00

I have a problem appending the imageNameArray where it is suppose to display file

  • 0

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 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.

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.

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.

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 🙂

Below is the javascript code where the appending occurs:

    <?php
    session_start();

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

    ?>

<script type="text/javascript">

 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 = <?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;
    }

</script>

<body>

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



</body>

Below is the php script (imageupload.php) 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;
        }
    }
}

$_SESSION ['fileImage'][] = array('name' => $_FILES ['fileImage']['name']);
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:11:27+00:00Added an answer on June 3, 2026 at 3:11 am

    The problem is that you are outputting the name of the uploaded file when the PHP code renders the page, which is before the (next) image is uploaded.

    This happens on this line in the stopImageUpload() JavaScript function:

    imageNameArray = <?php echo $output ?>;
    

    There are a couple ways you can resolve this, but I would go for the straightforward way of passing the file name back via the stopImageUpload() function.

    // stolen from http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding
    function htmlEncode(value) { return $('<div/>').text(value).html(); }
    
    function stopImageUpload(success, filename) {
      var imageNameArray = new Array();
      var result = '';
    
      if (success) {
        result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
    
        $('.listImage').append(htmlEncode(filename) + '<br/>');
      } else {
        result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
      }
    }
    

    Then you simply need to adjust the last line of imageupload.php:

    <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');</script>
    

    Note: If you have the option turned on to allow <?= ?> blocks you can make the above code look much nicer:

    <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?= $result ? 'true' : 'false'; ?>, <?= $_FILES['fileImage']['name'] ?>);</script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem appending the imageNameArray where it is suppose to display file
I have a peculiar problem. I am loading a JSON file that contains the
I have problem with TableView its empty. In (.h) file: @interface TableViewController : UITableViewController{
I am having the following problem: I have a WCF service that checks a
I have a rather interesting problem. I have a parent page that will create
I'm trying to solve the following problem: Say I have a Python script (let's
I have a script that scans ports for open proxy servers. Problem is if
I have a weird problem. I generate a HTML page, hosted let's say at
i have now since a while a systematic problem with NSStrings and appending these
I have a file named files, which contain 100-1-0_Message1_Tableau_problem.txt 1001-1-0_EDM_Queries_v2.mdb 1001-1-0_geocodes.xlsx 1001-1-0_losstypes.xlsx 1001-10-0_Exposure_Analysis_Tables_Needed.xlsx I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.