Sussudio
07-13-2012, 09: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?
uploader.js
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!');
files.html('');
//Add uploaded file to list
if (response === "success") {
jQuery('#photo').val('http://domain.com/path/to/image/' + file);
jQuery("#status").fadeOut();
} else {
alert('Uh-oh, something went wrong"!');
}
}
});
});
photo.php
$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";
}
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?
uploader.js
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!');
files.html('');
//Add uploaded file to list
if (response === "success") {
jQuery('#photo').val('http://domain.com/path/to/image/' + file);
jQuery("#status").fadeOut();
} else {
alert('Uh-oh, something went wrong"!');
}
}
});
});
photo.php
$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";
}