Multiple file upload incomplete

I am using the form plugin and an own plugin where I grab the files from the file upload to send them to an online application form. Everything works well so far except when submitting for example 5 files via Drag and Drop the final output shows only 3 files. When I dump the form or formdata it only shows that 3 of the 5 files have been properly moved from the tmp to my destination folder where I grab them by path and name to send them further. Any idea why this is? Maybe my script needs to queue up to kind of wait before the form plugin has moved all the files to the final destination before I can get them from that folder?

If you remove your own custom plugin which interferes with the form processing and just upload the files, do they work or you get the same problem?

Thanks for getting back to me on this. Yes without my plugin it’s working but I somehow need to hook into the form processing to get the files sent to the other online service. I also can pretty much imagine where the problem comes from. When uploading files via the form they get processed and moved to the destination folder. However my plugin seems to hook in to early so it starts to read the file names from the final destination folder without having the original form plugin processed all of them. (At least this is what I imagine what could be the problem). For better understanding, here is part of my plugin:

<?php
namespace Grav\Plugin;

use Grav\Common\Data\Data;
use Grav\Common\Data\Blueprint;
use Grav\Common\Grav;
use Grav\Common\Iterator;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Common\Utils;
use RocketTheme\Toolbox\Event\Event;

class CustomPlugin extends Plugin{
    
    public static function getSubscribedEvents()    {
        return [ 
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }

    public function onPluginsInitialized()
    {
        if ($this->isAdmin()) {
            $this->active = false;
            return;
        }

        $this->my_config = (array) $this->config->get('plugins.custom');

        $this->enable([
            'onFormProcessed' => ['onFormProcessed', 0]
        ]);
    }

    public function onFormProcessed(Event $event){
        $form = $event['form'];
        $action = $event['action'];
        $params = $event['params'];
        $grav = Grav::instance();
        $config = $grav['config'];
        
        if (!$this->active) {
            return;
        }

        switch ($action) {
            case 'myaction':
                if(isset($_POST)) {  
                    $owndata = array();
                    $formdata = $form->value()->toArray();

                     // some more form data will be retrieved and pushed into $owndata array. works all good...

                     if (empty($formdata['files_to_upload'])) {
                        $this->grav->fireEvent('onFormValidationError', new Event([
                            'form' => $form, 
                            'message' => $this->grav['language'] >translate('PLUGIN_FORM.FILE_VALIDATION', null, true)
                            ]));
                        $event->stopPropagation();
                        return;   
                    } else {
                        $domain_remote = 'subdomain.remoteservice.com';
                        $domain_host = $this->my_config['host'];
                        $upload_folder = $this->my_config['uploadfolder'];
                        foreach ($formdata['files_to_upload'] as $file) {
                            $file_with_path = $domain_host . "/" . $upload_folder . "/" . $file['name'];
                            $owndata[] = "files[] = ".urlencode($file_with_path);
                        }
                    }

                     // preparing for ssl
                     $errno  = $errstr = 0;
                     $query  = implode("&",$owndata);
                     $sock   = fsockopen("ssl://".$domain_remote, 443, $errno, $errstr, 30);

                    if(!$sock) {
                        die($errstr);
                    } else {
                        // Sende Informationen an den Server
                        fputs($sock, "POST /some/url/save/ HTTP/1.1\r
");
                        fputs($sock, "Host: ".$domain_remote."\r
");
                        fputs($sock, "Content-type: application/x-www-form-urlencoded; charset=iso-8859-1\r
");
                        fputs($sock, "Content-length: ".strlen($query)."\r
\r
");
                        fputs($sock, $query);
                        fputs($sock, "Connection: Close\r
\r
");

Plugin code has not been copied complete here but the essential parts are given to see why I need the final destination path for the uploaded files because the ssl connection demands an absolute accessible file path to establish the connection.

And on a side note - the form validation fails for a multiple file upload field when set to true. It still demands a file even when one is given. But that bug has already been pointed out and there is already an issue on Github. Again - thanks for your help.