简体   繁体   English

CSV文件未通过php和jquery(ajax)上传

[英]CSV File not uploading through php and jquery(ajax)

I am trying to upload a csv file using this code but the file directory is not getting uploaded to the folder and i cannot see any error in the code. 我正在尝试使用此代码上传一个csv文件,但文件目录没有上传到该文件夹​​,并且我在代码中看不到任何错误。 Please help me in this. 请帮助我。

HTML code: HTML代码:

<form method="POST" action="submit.php" enctype="multipart/form-data""> <input type="file" name="file" id="upload"><br> </form>

JQUERY AJAX code: JQUERY AJAX代码:

$(document).ready(function(){
    $('#upload').on('change', function(e) {
        e.preventDefault();
        var file=this.files[0];
        console.log(file);    
        var filename = file.name;
        var ext = filename.split('.')[1];
        if(ext == "csv"){
            FileUploadAjaxCall();
        }else{

            $("#fileMsg").html("Extension not valid:Try Again");
        }                     
    });
});

function FileUploadAjaxCall(){  
    $.ajax({
            url:'submit.php',
            type:'POST',
            data:new FormData($('#upload').get(0)),
            contentType:false,
            cache:false,
            processData:false,
            success:function(data){
                if(data == 1){
                    console.log("File Uploaded Successfully");
                }else{
                    console.log(data);
                    console.log("Error");
                }
            }
        });
}`

PHP code: PHP代码:

<?php    

        $file = $_FILES['file'];

        $filename = $_FILES['file']['name'];
        $filetmpname = $_FILES['file']['tmp_name'];
            $fileDes = 'uploads/'.$filename;
            $t = move_uploaded_file($filetmpname,$fileDes);
            if($t == true){
                echo 1;
            };

?>

I have re-written your code as follows. 我已将您的代码重新编写如下。 Its always good to validate files upload at server backend and javascript validation can be fooled. 验证在服务器后端上传的文件总是很有益的,并且可以欺骗javascript验证。

Try the code below and let me see what happens 试试下面的代码,让我看看会发生什么

<html>
<head>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function (e) {
    $("#uploadForm").on('submit',(function(e) {
        e.preventDefault();
        $.ajax({
            url: "upload_processor.php",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            success: function(data)
            {
            $("#targetLayer").html(data);
                        $("#errorLayer").html(data);
            },
            error: function() 
            {
            }           
       });
    }));
});
</script>
</head>
<body>
<div>
<form id="uploadForm" action="upload_processor.php" method="post">
<div id="targetLayer"></div>

<div id="errorLayer"></div>

<div id="uploadFormLayer">
<input name="file" type="file" class="inputFile" /><br/>
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</div>
</body>
</html>




<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['file']['tmp_name'])) {

$fileName   = $_FILES['file']['name'];

$sourcePath = $_FILES['file']['tmp_name'];
$targetPath = "uploads/".$_FILES['file']['name'];

if($fileName ==''){
echo "<div id='errorLayer'>Please select file</div>";
exit;
}

$fileType = pathinfo($fileName, PATHINFO_EXTENSION);
            $allowTypes = array('csv');
            if (!in_array($fileType, $allowTypes, true)) {
               echo "<div id='errorLayer'>File type is invalid. Only CSV is allowed</div>";
exit;
            }


if(move_uploaded_file($sourcePath,$targetPath)) {
echo "<div id='targetLayer'>File  uploaded successfully</div>";
?>

<?php
}
}
}
?>

//form.html //form.html

<form method="POST" action="submit.php" id="uploadForm">
    <input type="file" name="file" id="upload" accept=".csv"><br>
    <input type="submit">
</form>
<div id="fileMsg"></div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
   $(document).ready(function(e){
   $('#uploadForm').on('submit', function(e) {
       e.preventDefault();
        $.ajax({
            url:'submit.php',
            type:'POST',
            data:new FormData(this),
            contentType:false,
            cache:false,
            processData:false,
            success:function(){
                    $("#fileMsg").html("File Uploaded Successfully");
                },
            error:function(){
                    //$("#fileMsg").html(data);
                    $("#fileMsg").html("Error");
                }

        });
     });
   });

 </script>

//submit.php //submit.php

<?php

  //  $file = $_FILES['file'];

    $filename = $_FILES["file"]["name"];
    $filetmpname = $_FILES["file"]["tmp_name"];
        $fileDes = "uploads/".$filename;
        $t = move_uploaded_file($filetmpname,$fileDes);
        if($t == true){
            echo "1";
        };

  ?>

u can do it like this 你可以这样做

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

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