我没有使用 php,ajax 获得图像上传的临时名称


I dont get temp name of image upload using php,ajax

我想使用 php 和 ajax 获取图像上传的临时名称。我得到了upolad图像的文件名。但是不要获取图像上传的临时名称。我的代码如下

主.php

<form action=" " method="POST" enctype="multipart/form-data">
    <div class="form-group">
        <label for="exampleInputFile">File Upload</label>
        <input type="file" name="file" id="file" size="150">
        <p class="help-block">Only Excel/CSV File Import.</p>
    </div>
    <button type="button" class="btn btn-default"
            name="Import" value="Import" onclick="file_up()">
        Upload</button>
</form>
<div id="upload_show"></div>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
    function file_up() {
        var file = $('#file').val();
        $.ajax({
            type: "POST",
            url: 'first_ajax.php',
            data: 'file=' + file,
            success: function (msg){
                $("#upload_show").html(msg);
            }
        });
    }
</script>

first_ajax.php

<?php
   echo $file1 = $_POST['file'];   // for get file name
   echo $file1 = $_POST['file']['tmp_name']; //for get temp name
?>

而不是,
var file = $('#file').val();

尝试
var file = $("#file").prop("files")[0];

使用 FormData 在 AJAX 文件中获取 $_FILES 和 $_POST 数组...

function file_up() {
 var formData = new FormData($('#form')[0]);
                    $.ajax({
                        url: 'first_ajax.php',
                        type: 'POST',
                        data: formData,
                        async: false,
                        success: function(data) {
                            $("#upload_show").html(msg);
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                    });
                }