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 9137023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:02:16+00:00 2026-06-17T09:02:16+00:00

I have an ajax application. It basically uploads images to a folder then PHP

  • 0

I have an ajax application. It basically uploads images to a folder then PHP grabs them and puts them into an array. From there it is json_encodeded and sent back through echo. alert(responseText) gives me the json version. After I set a variable to json.parse(), with console.log() it comes out fine as an array. The array looks basically like this:

1 IMAGE1.jpg
2 IMAGE2.jpg
3 IMAGE3.jpg
4 IMAGE4.jpg
5 IMAGE5.jpg

etc.

I understand it is now in a javascript/json object. Unfortunately I can’t find any information about manipulating this object to grab all the image names and the last array type which is a true success or failure of the upload. Can anyone point me to some documentation or a way to manipulate this and extrapolate the information into an array. In the end i’m trying to dynamically show these images but I am assuming through the upload they do not all have the same name.

So my hope is to grab all .jpg/.png./gif etc. and grab those filenames and then using innerHTML create a bunch of <img> tags with the correct filename using a loop. As well, handling the last piece in the array which is just text saying if the upload was fully successful or not.

Should I not be using JSON? My code is below.

PHP

$dirhandler = opendir(UPLOAD_DIR);
//create handler to directory

//read all the files from directory
$nofiles=0;
while ($file = readdir($dirhandler)) {
    //if $file isn't this directory or its parent
    //echo "FILE SYSTEM: " . $file . "<br/>";
    //add to the $files array
    if ($file != '.' && $file != '..') {
        $nofiles++;
        $files[$nofiles]=$file;
    } //end of if loop
} //end of while loop
//json_encode($files);
if (!$success) { 
    $nofiles++;
    $files[$nofiles] = "Unable to upload file";
    exit;
} else {
    $nofiles++;
    $files[$nofiles] = "File Upload Successfully";
}
echo json_encode($files);

JAVASCRIPT

   if (xhr.readyState === 4 && xhr.status === 200) {
            progressBar.value="100";
            progressText = "Upload Complete!";
            progressStatus.innerHTML = progressText;
            var imageNames = JSON.parse(xhr.responseText);
            alert(imageNames);
        } else if (xhr.readyState === 0 || xhr.readyState === 1) {
            progressBar.value = "25";
            progressText = "Starting Upload..";
            progressStatus.innerHTML = progressText;
        } else if (xhr.readyState === 2) {
            progressBar.value = "50";
            progressText = "Almost Done...";
            progressStatus.innerHTML = progressText;
        } else if (xhr.readyState === 3) {
            progressBar.value = "75";
            progressText = "So Close!";
            progressStatus.innerHTML = progressText;
        } else if (xhr.status === 404 || xhr.status === 500) {
            alert("Your Upload Request failed.");
        }
  • 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-17T09:02:17+00:00Added an answer on June 17, 2026 at 9:02 am

    imageNames is a nothing-special object–use it like you would use any normal javascript object. E.g.: imageNames[2] will give you the second image name.

    However, you life will be much easier if you produce an array that json_encode() will understand as an actual JS array. In your PHP, include an entry for each image instead of using explicit indexes:

    $files = array();
    $entries = scandir(UPLOAD_DIR);
    foreach ($entries as $entry) {
        if (is_file(UPLOAD_DIR.'/'.$entry)) {
            $files[] = $entry;
        }
    }
    if (!$files) {
        // signal an error with an actual HTTP error code!
        header('Content-Type: application/json', false, 500);
        echo json_encode(array('error'=>'upload failed'));
    } else {
        header('Content-Type: application/json');
        echo json_encode($files);
    }
    

    However, I suspect that you should be gathering your file names when you process them. Use an upload form with input elements like <input type="file" name="images[]">, then upload to a script with this:

    function process_uploaded_files($files, $destdir, $nametmpl) {
        $moved = array();
        foreach ($files['error'] as $key => $error) {
            $newname = FALSE;
            if ($error===UPLOAD_ERR_OK) {
                $source = $files['tmp_name'][$key];
                $destname = sprintf($nametmpl, $key);
                $dest = "{$destdir}/{$destname}";
                if (move_uploaded_file($source, $dest)) {
                    // success!
                    $newname = $destname;
                }
            }
            $moved[$key] = $newname;
        }
        return $moved;
    }
    
    function filename_to_url($filename) {
        // or whatever
        return ($filename) ? '/images/'.$filename : $filename;
    }    
    
    $uploaded_files = process_uploaded_files($_FILES['images'], UPLOAD_DIR, 'Image%03d');
    
    // you should always return URLs of resources (not bare filenames) to be RESTful
    $new_urls = array_map('filename_to_url', $uploaded_files);
    header('Content-Type: application/json');
    echo json_encode($new_urls);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on on an AJAX chat application using php, mysql. It's
So basically I have my backbone application making an ajax GET call to my
My PHP+JS+Ajax application requires users to log in, then saves login data to $_SESSION.
hi i created a simple php/mysql/Ajax chat application and I have a few questions.
Basically my intention is to create web application. I have to use js, ajax,
I have several AJAX calls to web services in an ASP.Net application, basically I
I have a php application that makes use of a Listener class, which basically
I have an ajax application that uses hashbanging for navigation and I keep track
I have an AJAX intensive application that requires sending multiple AJAX requests rapidly or
I'm building an ajax application and I have a choice to make on how

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.