title: Adding Random Number to Uploaded File
body: <p>I'm using Valum's Ajax-Uploader script to upload files to my server. Because there's a good chance of the uploaded files have the same name I add a random number to the filename. The problem is that the ajax uploader is returning the original filename into the <code>input[type=text]</code> instead of the new filename with the random number added. I've tried <code>echo $file;</code> instead of <code>echo "success";</code> , but all that happens is that the file is uploaded, and the script returns with the pop-up error.</p>
  
  <pre><code>jQuery(function() {
      var url = "http://example.com/uploads/samples/";
      var button = jQuery('#up');
      new AjaxUpload(button, {
          action: 'upload.php',
          name: 'upload',
          autoSubmit: true,
          onSubmit: function(file, ext) {
              // do stuff while file is uploading
          },
          onComplete: function(file, response) {
              if (response === "success") {
                  $('input[type=text]').val(url + file);
              } else {
                  jAlert('Something went wrong!', 'Error!');
              }
          }
      });
  });
  </code></pre>
  
  <p><strong>upload.php</strong></p>
  
  <pre><code>&lt;?php
      $uploaddir = '/path/to/uploaddir/'; 
      $file = basename($_FILES['file']['name']);
  
       if($_FILES['file']['name']) {
        $file = preg_replace('/\s+/', '_', $file);
        $rand = rand(0000,9999);
  
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $rand . $file)) { 
              echo "success";
          } else {
              echo "error";
          }
      }
  ?&gt;
  </code></pre>