Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Problem with Ajax Uploader and Filenames

    I'm not sure whether this should be in the PHP or Javascript forums

    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"!');


    PHP Code:
    $uploaddir '/path/to/image/'
    $file $uploaddir time() . '_' basename($_FILES['uploadfile']['name']); 
    if (
    move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
    } else {

    Last edited by Sussudio; 07-13-2012 at 10:45 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,650 Times in 4,612 Posts

    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".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts