Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向上载的文件添加随机数_Php_Ajaxuploader - Fatal编程技术网

Php 向上载的文件添加随机数

Php 向上载的文件添加随机数,php,ajaxuploader,Php,Ajaxuploader,我正在使用Valum的Ajax上传脚本将文件上传到我的服务器。因为上传的文件很可能同名,所以我在文件名中添加了一个随机数。问题是ajax上传程序将原始文件名返回到输入[type=text]中,而不是添加了随机数的新文件名。我尝试了echo$file而不是回显“成功”,但所发生的一切是文件被上传,脚本返回并出现弹出错误 jQuery(function() { var url = "http://example.com/uploads/samples/"; var button =

我正在使用Valum的Ajax上传脚本将文件上传到我的服务器。因为上传的文件很可能同名,所以我在文件名中添加了一个随机数。问题是ajax上传程序将原始文件名返回到
输入[type=text]
中,而不是添加了随机数的新文件名。我尝试了
echo$file而不是
回显“成功”,但所发生的一切是文件被上传,脚本返回并出现弹出错误

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

更改

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


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

` /** * This function return unique string as a key */ public static function getUniqueKey(){ $currentDateTime = date("YmdHisu"); return $currentDateTime . BTITTools::createRandomString(3); } ` ` /** *此函数返回唯一字符串作为键 */ 公共静态函数getUniqueKey(){ $currentDateTime=日期(“YmdHisu”); 返回$currentDateTime.BTITTools::createRandomString(3); } `
@客户端代码和服务器代码完全独立存在。基本上,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";  
}  
?>

您知道吗,您只需以长格式编写一次
jQuery
?通过将代码包装在
(函数($){…})(jQuery)中
,无论是否使用了
noConflict
,都可以使用
$
。在代码中,您甚至可以在第一行中使用
jQuery(函数($){
,然后在函数中使用
$
。为什么要使用
addslashes()
?没有必要这样做。很抱歉,我粘贴了错误的代码。OP使用正确的upload.php代码进行了更新。脚本正在工作,但上载的文件附带了随机数,但输出到
$('input[type=text]')的文件名。text(url+文件);
缺少该数字。
<?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";  
}  
?>
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!'); 
    } 
}