Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在jquery中获取表单文件字段以上载图像_Php_Jquery_Ajax_File Upload - Fatal编程技术网

Php 如何在jquery中获取表单文件字段以上载图像

Php 如何在jquery中获取表单文件字段以上载图像,php,jquery,ajax,file-upload,Php,Jquery,Ajax,File Upload,我有下面的html This form allows you to upload a file to the server.<br> <div class="imguploadpath" style="width:100%;height:200px;background-color:#CCC;"> <form name="myFormName" id ="myFormId" action="" method="post">

我有下面的html

This form allows you to upload a file to the server.<br>
        <div class="imguploadpath" style="width:100%;height:200px;background-color:#CCC;">
          <form name="myFormName" id ="myFormId" action="" method="post">
             <input type="file" name="img" id = "image_pe">
             <br/><br/><br/><br/>
             <input id="cmdSubmit" value="Submit" type="submit" class="">
          </form>
        </div>
//我必须在jQueryAjax中将文件属性发送到php文件

 $.ajax({
            url: 'submit.php?files',
            type: 'POST',
            data: data,
            cache: false,
            dataType: 'json',
            processData: false, // Don't process the files
            contentType: false, // Set content type to false as jQuery will tell the server its a query string request
            success: function(data, textStatus, jqXHR)
            {
                if(typeof data.error === 'undefined')
                {
                    // Success so call function to process the form
                    submitForm(event, data);
                }
                else
                {
                    // Handle errors here
                    console.log('ERRORS: ' + data.error);
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                // Handle errors here
                console.log('ERRORS: ' + textStatus);
                // STOP LOADING SPINNER
            }
        });
       return false;

    });
但是
var文件
只返回一些临时路径
c://fakepath/image.jpg

如何在jquery和php中上传,我需要传递php文件中的表单字段,我知道
value()
jquery函数是错误的

正确的方法是什么

对于php文件上载,我正在考虑使用此脚本

尝试以下方法: JQuery:

PHP代码:

$file = $_FILES['image_pe'];

看看这个。这可能对你很有帮助


也许这会有帮助[[1]:希望有帮助……这也帮我找到了很多问题。这解决了jquery问题,但是你也可以添加php代码吗?谢谢
var formdata = new FormData();
            jQuery.each($('#image_pe')[0].files, function(i, file) {
             formdata.append('image_pe', file);
            });


            $.ajax({
                url: "/my.php",
                data : formdata,
                dataType : "json",
                type : "post",
                cache: false,
                contentType: false,
                processData: false,
                success: function(data){

                },
                failure: function(){
                    $(this).addClass("error");
                }
            });
            return false;
$file = $_FILES['image_pe'];