如何使用 PHP 通过 HTTP 发送文件?

问题描述 投票:0回答:0

不幸的是,我无法访问文件路径 - 我在 Moodle 中工作时只能访问文件句柄(例如

fopen(path)
的结果)。

现在,我想通过 HTTP 将此文件句柄发送到 Node.js 应用程序进行一些处理。这是我现在的 PHP 代码:

foreach ($files as $file) {
    //Only send files
    if ($file->get_mimetype()) {
        $fh = $file->get_content_file_handle();
        $postdata[] = [
            'name' => 'files[]',
            'contents' => $fh,
            'filename' => $file->get_filename(),
            'headers' => [
                'Content-Type' => $file->get_mimetype(),
            ],
        ];
    }
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, '...');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'Content-Type: multipart/form-data',
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);

curl_close($curl);

我是 PHP 的新手,所以这不起作用的原因可能有很多 - 但是,一些指导会很好。

php http moodle
© www.soinside.com 2019 - 2024. All rights reserved.