View Full Version : Problem with Ajax Uploader and Filenames

07-13-2012, 10:42 PM
I'm not sure whether this should be in the PHP or Javascript forums :confused:

I have a simple ajax image uploader that outputs the url of the uploaded image into a textarea when finished. It was working perfect before I modified the corresponding .php file to prepend a time stamp to the file name. How do I modify the .js file to show the proper filename with the timestamp?


jQuery(function() {
var btnUpload = jQuery('#up');
var mestatus = jQuery('#status');
var files = jQuery('#photo');
new AjaxUpload(btnUpload, {
action: 'photo.php',
name: 'uploadfile',
onSubmit: function(file, ext) {
status.html('<img src="ajax-loader.gif">');
onComplete: function(file, response) {
alert('Image uploaded!');
//Add uploaded file to list
if (response === "success") {
jQuery('#photo').val('http://domain.com/path/to/image/' + file);
} else {
alert('Uh-oh, something went wrong"!');


$uploaddir = '/path/to/image/';
$file = $uploaddir . time() . '_' . basename($_FILES['uploadfile']['name']);

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "success";
} else {
echo "error";

Old Pedant
07-13-2012, 10:51 PM

If all your PHP code sends back to your web page is either "success" or "fail" then how in the world do you expect the JavaScript code to *GUESS* what that new file name is?

After all, the JS code has no idea what time of day it is on your server. It might be 2 seconds off or it might be several hours off if they are in different time zones.

The PHP code *MUST* SEND BACK the new file name and the JS code must then expect and check for a file name instead of just "success".