【问题标题】:Access file from PHP backend从 PHP 后端访问文件
【发布时间】:2020-06-13 01:52:47
【问题描述】:

我有一个带有 Angular 前端和 PHP 后端的简单应用程序。我在前端上传一个文件,需要在后端处理它。 我的角度代码看起来像,

const formData = new FormData();
    formData.append('file', this.selectedFile, 'transaction_file');
    this.http.post('http://localhost/MyBackend/index.php', formData).subscribe(res => {
      console.log(res);
      alert('success!');
    });

并且在我的后端项目的index.php 中,我尝试使用此文件访问此文件,

$postdata = file_get_contents("php://input");

但是这个 $postdata 似乎是空的。非常感谢任何如何在后端获取此文件的想法。

【问题讨论】:

    标签: php angular file-upload


    【解决方案1】:

    我不确定this.http 是什么,但看起来您正在创建POST 请求,该请求可以在PHP 中使用变量$_POST 处理,文件位于$_FILES 变量中。


    $_POST
    通过 HTTP POST 方法传递给当前脚本的变量关联数组。
    来源:php.net


    $_FILES
    通过 HTTP POST 方法上传到当前脚本的项目的关联数组。
    来源:php.net


    在 w3schools.com 上有 basic php file upload tutorial

    【讨论】:

    • 我试过 $_FILES 但它似乎是空的。是的,我的 this.http 是一个有角度的 HttpClient 对象,它正在向 php 后端发送一个 post 请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多