检查文件是否已存在于jquery中

时间:2016-05-20 08:41:15

标签: php jquery

如何检查使用jQuery File Uploader文件名的上传文件是否已存在。我尝试了下面的代码,它一直覆盖上传的第一个文件。我想要的是防止添加文件。有人可以指导我吗?感谢。

代码:

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

    // This is for custom errors;   
    // $custom_error= array();
    // $custom_error['jquery-upload-file-error']="File already exists";
    // echo json_encode($custom_error);
    // die();

    $error =$_FILES["myfile"]["error"];
    //You need to handle  both cases
    //If Any browser does not support serializing of multiple files using FormData() 
    if(!is_array($_FILES["myfile"]["name"])) //single file
    {
        $fileName = $_FILES["myfile"]["name"];
        // check if fileName already exists
        if (file_exists($fileName)) {
         $fileName = $_FILES["myfile"]["name"];
         echo "string"; 
        } else {
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
        $ret[]= $fileName; 
        }
    }
    else  //Multiple files, file[]
    {
      $fileCount = count($_FILES["myfile"]["name"]);
      for($i=0; $i < $fileCount; $i++)
      {
        $fileName = $_FILES["myfile"]["name"][$i];
        move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
        $ret[]= $fileName;
      }

    }
    echo json_encode($ret);
 }

JS:

    $(document).ready(function() {

  var baseurl = window.location.protocol + "//" + window.location.host + "/rmc/";
  var newurl = baseurl + 'document_items/upload';
  var deleteurl = baseurl + 'document_items/delete';

  $("#fileuploader").uploadFile({
   url            : newurl,
   fileName       : "myfile",
   returnType     : "json",
   multiple       : true, //allow multiple file upload
   showFileSize   : false, // show file size
   acceptFiles    : "image/*,application/pdf", // files accepted list
   formData: {"name":"Ravi","age":31},
   showAbort      : true, // display abort button on upload
   onSuccess:function(files,data,xhr) {
     // $("#status").html("<font color='green'>Upload is success</font>");
     },
     afterUploadAll:function() {
      swal({
       title     : "Success",
       text      : "File(s) successfuly uploaded.",
       type      : "success"
      });
     },
    onError: function(files,status,errMsg)
    {  
     swal({
       title     : "Error",
       text      : "Aw Snap! Something went wrong.",
       type      : "error"
      });
    },
   deleteCallback: function (data, pd) {
     for (var i = 0; i < data.length; i++) {
       $.post(deleteurl, {op: "delete",name: data[i]},
         function (resp,textStatus, jqXHR) {
           // Show Message
           swal("Success!", "File deleted successfuly!", "success");
         });
       }
       pd.statusbar.hide(); // You choice.
     }
  });
});

1 个答案:

答案 0 :(得分:1)

如下所示更改您的代码,然后重试。

if(!is_array($_FILES["myfile"]["name"])) //single file
{
        $fileName = $_FILES["myfile"]["name"];
        // check if fileName already exists
        if (file_exists($output_dir.$fileName)) {
         $fileName = $_FILES["myfile"]["name"];
         echo "string"; 
} else {
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
        $ret[]= $fileName; 
        }
}