繁体   English   中英

如何使用uploadify为每个上传的文件获取唯一的名称

[英]How to get a unique name for each uploaded file using uploadify

我正在使用jQuery uploadify插件上传文件。 所有文件都上传到同一目录。 当我尝试上传两次文件时,出现以下错误。

filename.gif (4.3KB) - IO Error

我想每次上传一个具有唯一名称的文件。 还有许多其他用户在同一目录中上载文件。 因此,两个用户有可能共享相同的文件名。 如何避免被覆盖。

我的代码:

$('.SingleFileUpload').uploadify({
        'uploader'  : '/uploadify/uploadify.swf',
        'script'    : '/uploadify/uploadify.php',
        'cancelImg' : '/uploadify/cancel.png',
        'folder'    : '/uploads',
        'auto'      : true,
        'queueID'   : 'fileQueue',
        'removeCompleted':false,
        'onComplete'  : function(event, ID, fileObj, response, data) {
                            $(event.target).closest('form').append( '<input type="hidden" name="uploaded_file" value="' + response + '">' );
                        }
      });

首先,拒绝重复的文件名:

$targetFolder = '/uploads'; // Relative to the root

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        if (file_exists($targetFile)){
            echo 'File does already exist, choose another name!';
        }
        else {
            move_uploaded_file($tempFile,$targetFile);
            echo '1';
        }
    } else {
        echo 'Invalid file type.';
    }
}
?>

您可以在所有文件名前添加用户ID +下划线(或将ID与文件名分开的任何字符,以避免UID1 + 2file == UID12 + file

除了强迫用户选择其他名称之外,您还可以实现自动的名称更改:通过添加前缀和/或后缀,或通过计算文件的哈希值。 最后一个选项还可以防止重复文件(相同名称,相同内容)出现在服务器上。

我不认为使用javascript对此有好处还是安全,我使用的一种方法是通过在服务器端计算其SHA1来命名每个文件。

我正在复制文件名,并在最后添加copynr。 例如:filename(2)filename(3)filename(4)等。

$('#upload').uploadify({
        'uploader'       : 'resources/plugins/uploadify/uploadify.swf',
        'script'         : 'resources/plugins/uploadify/uploadify.php',
        'cancelImg'      : 'resources/plugins/uploadify/cancel.png',
        'folder'         : 'uploads/',
        'auto'           : true,
        'multi'          : false,
        'fileDesc'       : '*.jpg;*.jpeg;*.png;*.gif;',
        'fileExt'        : '*.jpg;*.jpeg;*.png;*.gif;',
        onComplete: function (evt, queueID, fileObj, response, data){
            alert(response); // = uploaded filename
        }

只是uploadify JS部分(不使用check方法!)现在uploadify.php部分

if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
    $ext = '.'.pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
    $filename = substr($_FILES['Filedata']['name'],0,-strlen($ext));
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile =  str_replace('//','/',$targetPath) . $filename . $ext;

    while(file_exists($targetFile)){
        preg_match('/\([0-9]{1,}\)$/', $filename,$matches);
        if(!empty($matches)){
            preg_match('/[0-9]{1,}/', $matches[0],$nr);
            $filename = substr($filename,0,-strlen($matches[0])) . '('.(((int)$nr[0])+1).')';
        }else
            $filename .= '(2)';
        $targetFile = str_replace('//','/',$targetPath) .$filename . $ext;
    }

if(!is_dir($targetPath)) mkdir(str_replace('//','/',$targetPath), 0755, true);

move_uploaded_file($tempFile,$targetFile);
echo $filename.$ext;}

HTH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM