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.");
}
JSON.parse
? – h2ooooooo Jan 15 at 18:21JSON.parse
is available on any modern browser. – Francis Avila Jan 15 at 18:22console.log(xhr.responseText);
, it'll probably look something like[,"Image1",...]
: it's an standard JavaScript array after it's been JSON.parsed. Google MDN Array for some documentation on what you can do with JS arrays. The reason the zero-index is empty, btw, is because you increment$nofiles
each time before adding an element to the$files
array, so the zero'th index remains empty – Elias Van Ootegem Jan 15 at 18:31