Adding Random Number to Uploaded File

徘徊边缘 提交于 2019-12-12 03:56:29

问题


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 input[type=text] instead of the new filename with the random number added. I've tried echo $file; instead of echo "success"; , but all that happens is that the file is uploaded, and the script returns with the pop-up error.

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

upload.php

<?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";
        }
    }
?>

回答1:


Client code and server code exist completely independently of each other. Basically, your JavaScript code has no way of knowing what your PHP code just did. url doesn't update automatically when you change it in the PHP file.

An alternate solution would be to have your PHP code echo the new filename, or echo an error message.

For example:

PHP

<?php
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $rand . $file)) {   
    // Everything went well! Echo the dollar sign ($) plus the new file name
    echo '$' . $_FILES['file']['tmp_name'], $uploaddir . $rand . $file;   
} else {  
    echo "error";  
}  
?>

JS

onComplete: function(file, response) { 
    // If the first character of the response is the "$" sign
    if (response.substring(0,1) == "$") { 
        $('input[type=text]').val(response.substring(1));
    } else { 
        jAlert('Something went wrong!', 'Error!'); 
    } 
} 



回答2:


Change

$('input[type=text]').text(url + file);

To

$('input[type=text]').val(url + file);



回答3:


I would like to suggest using date and time to create a unique random number. You can use following function in your code, I am fully trusted on this function because I already use this in my project.

`
    /**
    * This function return unique string as a key 
    */
    public static function getUniqueKey(){      
        $currentDateTime = date("YmdHisu");
        return $currentDateTime . BTITTools::createRandomString(3);
    }
`

@ My Code Programs



来源:https://stackoverflow.com/questions/12846552/adding-random-number-to-uploaded-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!