Mark
Mark

Reputation: 312

Struggling To Use PyroCMS Files Library To Upload Multiple Files

Am cross posting this with the PyroCMS forum in the bid to reach a wider audience for help as I just can't seem to fathom it.

When I try to upload multiple files using the PyroCMS Files library I run into problems. I can seemingly get files to upload singularly when calling the library and I can also get files to upload when bypassing the library and testing via move_uploaded_file. I've created the following (slightly stripped down) code in my controller:

public function upload($id)
{
    // Folder selected or redirect
    $id or redirect('admin/mis/resources');

    // Run on validation
    if ($_FILES)
    {
        if (count($_FILES['userfile']['name'] > 0))
        {                
            foreach ($_FILES['userfile']['name'] as $file)
            {                    
                $upload = Files::upload($id, $file);
            }
        }                                  

        redirect('admin/vle/resources/contents/'.$id);            
    }

    $this->template->build('admin/resources/upload');
}

However when running this code, no file is uploaded. If I output the upload I get the following message:

Array ( [status] => [message] =>

You did not select a file to upload.

I seems that I cannot for the life of me work out how to pass more than one file to Files::upload. I sure I'm missing something. However it's not that I cannot get files to upload in general because if I change the code to this locally for testing purposes my files are uploaded just fine:

if($_FILES)
{
    if(count($_FILES['userfile']['name'])) 
    {
        foreach ($_FILES['userfile']['name'] as $file) 
        {
            $img = "c:/wamp/www/testupload/files/".$file;

            move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $img);
        }
    }   
}

The upload method of the files library is here https://github.com/pyrocms/pyrocms/blob/2.2/develop/system/cms/modules/files/libraries/Files.php#L333

I have tried lots of different things to the point where my head is spinning somewhat now trying get to grips with this. So if anyone can help it'd be appreciated.

Upvotes: 1

Views: 1434

Answers (1)

Jerel Unruh
Jerel Unruh

Reputation: 46

The Files library doesn't currently support multiple files uploaded by one element. So it is trying to find a single file from the "userfile" file input instead of an array of files.

This should work:

<form method="post" action="/upload/1" enctype="multipart/form-data">
    <input name="userfile" type="file"/>
    <input name="secondfile" type="file"/>
</form

public function upload($folder_id)
{
    $upload = Files::upload($folder_id, 'Descriptive Name', 'userfile');
    $second_upload = Files::upload($folder_id, 'Some Name', 'secondfile');
}

The admin panel of PyroCMS uploads multiple files by using the BlueImp jQuery uploader. Perhaps a JS solution like that would work well for you? That way users get progress bars informing them of each upload's status and they can see when each upload completes.

Upvotes: 3

Related Questions