使用ajax和php发送文件

时间:2018-06-30 05:57:22

标签: php jquery ajax

我正在尝试发送任何类型的文件,例如图像或任何格式。我可以在jquery中获取文件值,但无法在php中接收该文件。请帮助我。 我的代码下面/....

index.html

<form id="contact-form" action="" method="post" role="form" enctype="multipart/form-data">
                            <div class="error-container"></div>
                            <div class="row">
                                <div class="col-xs-12 col-md-12">
                                    <div class="form-group">
                                    <input class="form-control form-control-name" name="name" id="name" placeholder="Name" type="text" required>
                                    </div>
                                </div>
                                <div class="col-xs-12 col-md-12">
                                    <div class="form-group">
                                        <input class="form-control form-control-email" name="email" id="email" placeholder="Email" type="email" required>
                                    </div>
                                </div>
                                <div class="col-xs-12 col-md-12">
                                    <div class="form-group">
                                        <input class="form-control form-control-subject" name="subject" id="subject" 
                                        placeholder="Position Applying For" required>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <textarea class="form-control form-control-message" name="message" id="message" placeholder="Message" rows="5" required></textarea>
                            </div>
                            <div class="form-group">
                            <p>Send us your resume ("doc","docx" or "pdf")</p>
                            <input type="file" id="file_input" name="file">
                            </div>
                            <div id="wait" style="text-align: center !important;width: 100%;display: none;padding-top: 20px;"><img src='images/loader.gif' style="text-align: center!important;margin: auto!important;" width="64" height="64" /><br>Loading..</div>
                            <span class="contact100-form-title" style="font-size: 16px;padding-top: 20px;padding-bottom: 15px;" id="contact_msg">
                            </span>
                            <div class="text-right"><br>
                                <button class="btn btn-primary solid blank" type="button" id="submit_form">Apply Now</button> 
                            </div>
                        </form>

Ajax代码:-

$('#submit_form').click(function(e) {
                var myFile = $('#file_input').prop('files');
                $.ajax({
                    type: "POST",       
                    enctype: 'multipart/form-data',
                    url: 'ajax/application.php',
                    async: false,
                    processData: false,
                    contentType: false, 
                    data:{myFile:myFile},
                    success: function(result) {
                        alert(result);

                    }
                });
            });

php代码:

<?php 
print_r($_POST);
print_r($_FILES);

 ?>

在php文件中显示数组为空..... 我没有在php文件中获取文件。

1 个答案:

答案 0 :(得分:-1)