【问题标题】:how to access $_FILES parameters while trying to do File Upload without Form?尝试在没有表单的情况下进行文件上传时如何访问 $_FILES 参数?
【发布时间】:2017-04-28 08:04:17
【问题描述】:

我已经编写了一个功能,我想在不使用任何表单的情况下上传表单,我在输入类型 = 带有 id 的文件的更改事件上调用 ajax 事件,我在 ajax 文件“上传”中获取 $_FILES 数据.php' 使用 jQuery 的 POST 方法。输入标签不在任何表单标签内。它独立存在。以下是我的代码:

我的纯 html 输入 type='file' 没有表单:

<div class="profile_pic">
 <img id="upload-button" class="profile-pic img-circle profile_img" src="img/default-image.png" alt="default">
 <input class="file-upload" type="file" name="profile_pic" id="profile_pic" />
</div>

我的 Javascript 代码是:

  var readURL = function(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#upload-button').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
 $(".file-upload").on('change', function(){
        readURL(this);

        alert('working');
            var file = profile_pic.files[0];
            var formData = new FormData();
            formData.append('formData', file);

            $.ajax({
                url: "uploadFile.php",    
                type: "POST",
                processData: false,
                contentType: false,
                dataType : 'json',
                data: formData,
                success: function (data) {
                  alert(data);
                  }
            });
    });
    $("#upload-button").on('click', function() {
       $(".file-upload").click();
    });

上传文件.php代码如下:

<?php
 print_r($_FILES);
 ?>

以下文件数据正在收到警报:

Array
(

 [formData] => Array
 (
   [name]=> Tulips.jpg
   [type]=> image/jpeg
   [tmp_name]=> chroot/tmp/phpipdF97
   [error]=> 0
   [size]=> 620888
 )

)

如何访问这些文件数据,以便使用 php 的 move_uploaded_file() 将文件上传到目标文件夹路径?提前致谢。

【问题讨论】:

    标签: file-upload jquery-file-upload


    【解决方案1】:

    我得到了解决方案。我已经访问了“uploadFile.php”中的 $_FILES 值,如下所示:

    $_FILES["formData"]["tmp_name"]
    $_FILES["formData"]["name"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多