2011-10-09 58 views
2

我使用jQuery插件uploadify上传文件以获得每个上传的文件的唯一名称。所有文件都上传到同一个目录。当我尝试上传文件两次时,它会给我以下错误。如何使用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 + '">'); 
         } 
     }); 

回答

2

我不使用JavaScript是好是安全的这一想,一个方法我用的是命名服务器端的每个文件通过计算其SHA1

+0

确定。 'php'标签被添加。因此,我们可以解决它** ** uploadify.php我 – Student

3

要开始了,拒绝重复的文件名:

$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(或标识从文件名中分离的任何字符前缀的所有文件名,以避免UID1 + 2file == UID12 + file

您可以通过添加前缀和/或后缀,或者通过计算文件的哈希来代替强制用户选择另一个名称,也可以实现自动更名:最后一个选项也是从服务器防止出现重复的文件(名称相同,相同的内容)。

+0

也想补充一个时间戳,而你在它。 –

+0

我提出了两个方案:要么通过让用户自由选择的文件名(“漂亮” /“可读”的文件名,从而使用户可以轻松地重提它),或强制唯一的名称(建议服务器)。 –

+0

感谢您的回复。所以这个代码是** uploadify.php **。是否需要在** uploadify.php **中初始化'$ targetFolder'?我们有一个选择'文件夹'来设置它在jQuery中。 – Student

1

我复制的文件名,并在末尾添加copynr。像: 文件名(2) 文件名(3) 文件名(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部分 现在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

+0

我正在进行类似的设置,但对大文件有问题。如果'move_uploaded_file'需要很长时间,则不会从uploadify.php返回文件名。我甚至尝试将'successTimeout'设置为3分钟,但这也没有帮助。 – thomas

+0

你是否使用大文件的正确php.ini设置?请参阅http://stackoverflow.com/questions/12339519/sending-formdata-with-uploadify-not-working-with-large-file-size – sldev