【问题标题】:Adding Random Number to Uploaded File向上传的文件添加随机数
【发布时间】:2012-10-11 18:53:43
【问题描述】:

我正在使用 Valum 的 Ajax-Uploader 脚本将文件上传到我的服务器。因为上传的文件很有可能具有相同的名称,所以我在文件名中添加了一个随机数。问题是 ajax 上传器将原始文件名返回到 input[type=text] 而不是添加了随机数的新文件名。我试过 echo $file; 而不是 echo "success"; ,但发生的只是文件已上传,脚本返回并弹出错误。

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

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

【问题讨论】:

  • 你知道你只需要写一次长格式的jQuery吗?通过将代码包装在(function($) { .... })(jQuery); 中,无论noConflict 是否被使用,您都可以使用$。在您的代码中,您甚至可以在第一行使用jQuery(function($) {,然后在函数中使用$
  • 你为什么使用addslashes()?没有必要这样做。
  • 对不起,我粘贴了错误的代码。 OP 使用正确的 upload.php 代码更新。该脚本正在运行,但上传的文件附加了随机数,但输出到$('input[type=text]').text(url + file); 的文件名缺少该数字。

标签: php ajaxuploader


【解决方案1】:

改变

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

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

【讨论】:

    【解决方案2】:

    客户端代码和服务器代码完全独立存在。基本上,您的 JavaScript 代码无法知道您的 PHP 代码刚刚做了什么。 url 在 PHP 文件中更改时不会自动更新。

    另一种解决方案是让您的 PHP 代码回显新文件名,或回显错误消息。

    例如:

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

    【讨论】:

      【解决方案3】:

      我想建议使用日期和时间来创建一个唯一的随机数。 你可以在你的代码中使用以下函数,我完全信任这个函数,因为我已经在我的项目中使用了它。

      ` /** * 此函数返回唯一字符串作为键 */ 公共静态函数 getUniqueKey(){ $currentDateTime = date("YmdHisu"); 返回 $currentDateTime 。 BTITTools::createRandomString(3); } `

      @My Code Programs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 2013-02-12
        • 2016-06-30
        • 2014-01-16
        • 2013-05-09
        • 1970-01-01
        相关资源
        最近更新 更多