简体   繁体   English

检查文件是否已经存在于jQuery中

[英]Check whether the file already exists in jquery

how can I check if uploaded file using jQuery File Uploader filename already exists. 如何检查使用jQuery File Uploader文件名上传的文件是否已存在。 I tried below code and it keeps overriding the first file uploaded. 我尝试了下面的代码,它始终覆盖第一个上传的文件。 What I wanted is to prevent adding the file. 我想要的是防止添加文件。 Can someone guide me? 有人可以指导我吗? Thanks. 谢谢。

Code: 码:

$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: 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.
     }
  });
});

Change your code as below and try again. 如下更改代码,然后重试。

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

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

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