0

Unfortunately, I don't have access to the filepath - I only have access to the filehandle (e.g. the result of fopen(path)) as I'm working in Moodle.

Now, I want to send this filehandle over HTTP to a Node.js app for some processing. This is my PHP code right now:

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);

I'm new to PHP, so there are probably many reasons why this doesn't work - however, some guidance would be nice.

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
gullerg
  • 371
  • 2
  • 15
  • Have a look at https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php so you know roughly what you should be trying to aim for in terms of the curl code – ADyson May 03 '23 at 11:36
  • Thanks! Problem with the above is that it's still expects a path/to/file (it seems), whereas I only have the content of the file – gullerg May 03 '23 at 11:46
  • I'm not familiar with Moodle, but from a quick glace at the docs at https://moodledev.io/docs/apis/subsystems/files, surely you can call `$file->get_filepath()` to get the path? – ADyson May 03 '23 at 11:55
  • Yeah one would think, but this is unfortunately not the full path (only some kind of relative path) - https://moodle.org/mod/forum/discuss.php?d=331509 - maybe I can do the temp dir thing that they refer to in the discussion. In any case, it takes a lot of hacking around to get the Moodle stuff working haha. Thanks for your ideas! – gullerg May 03 '23 at 12:01
  • `some kind of relative path`...maybe you can prepend it with the root path of the document store, perhaps? That must be accessible somewhere?? Of course this assumes the document store is on disk, and not a remote service like S3 or something. Or yes, copying to a temp dir and then using that path for the upload (and then deleting from the temp dir afterwards) sounds like a reasonable workaround – ADyson May 03 '23 at 12:06
  • Yeah I have something like that working now - however, it feels very hacky and not sure if it's prod ready - we'll see! – gullerg May 03 '23 at 12:39
  • Does this answer your question? [how to upload file using curl with PHP](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – Lenny4 May 03 '23 at 15:42
  • You should use \moodle_url::make_pluginfile_url() to generate a url for a file - what files are they? eg: assignments? – Russell England May 05 '23 at 16:16

0 Answers0