2014-01-13 54 views
0

我刚安装了这个插件在我的网站和作品就像一个魅力: http://hayageek.com/docs/jquery-upload-file.phphayageek jQuery的上传文件的插件 - 重命名重复的文件

我唯一的麻烦是与现有的文件名上传文件时,它会覆盖没有任何警告/警报/重命名。

如何通过显示消息来防止文件覆盖,如果存在同名的文件?

想我必须采取行动对move_uploaded_file upload.php的,但我想不出如何

<?php 
//If directory doesnot exists create it. 
$output_dir = "../download/"; 

if(isset($_FILES["myfile"])) 
{ 
$ret = array(); 

$error =$_FILES["myfile"]["error"]; 


{ 

    if(!is_array($_FILES["myfile"]['name'])) //single file 
    { 
     $fileName = $_FILES["myfile"]["name"]; 
     move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]); 
     //echo "<br> Error: ".$_FILES["myfile"]["error"]; 

      $ret[$fileName]= $output_dir.$fileName; 
    } 
    else 
    { 
      $fileCount = count($_FILES["myfile"]['name']); 
      for($i=0; $i < $fileCount; $i++) 
      { 
      $fileName = $_FILES["myfile"]["name"][$i]; 
      $ret[$fileName]= $output_dir.$fileName; 
      move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName); 
      } 

    } 
} 
echo json_encode($ret); 


} 

?> 

回答

1

你可以在文件名上运行一个简单的file_exists()您移动它来检查,如果它存在之前,如果它你只是改变文件名到别的东西。

这里是从链路采取上述的一个代码段:

<?php 
    $img = "images/".$_FILES['bilde']['name']; 
    $t=0; 
    while(file_exists($img)){ 
    $img = "images/".$_FILES['bilde']['name']; 
    $img=substr($img,0,strpos($img,"."))."_$t".strstr($img,"."); 
    $t++; 
    } 
    move_uploaded_file($_FILES['bilde']['tmp_name'], $img); 
?>