<p>Have you tried naming your field <code>img[]</code>? So something like this...</p>
<p><code>&lt;input id=&quot;file-upload&quot; type=&quot;file&quot; name=&quot;img[]&quot; multiple=&quot;multiple&quot; /&gt;</code></p>
<p>There's a bit more info on this answer <a href="https://stackoverflow.com/a/12006515/1809628">here</a>. That should hopefully get you to where you need to be.</p>

CC BY-SA 4.0
2024-08-16T15:46:30.787Z
2024-08-16T16:06:46.820Z
2024-08-16T16:06:46.820Z
Steve Holland
<p>I need help setting up a form in CRAFT CMS. There is a form:</p>
<pre><code>&lt;form id=&quot;add&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
    {{ csrfInput() }}
    {{ actionInput('families/families/start') }}
    &lt;div class=&quot;fields-boxes&quot;&gt;
        &lt;div class=&quot;field&quot;&gt;
            &lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;file-field&quot;&gt;
            &lt;input type=&quot;file&quot; name=&quot;img&quot; /&gt;
        &lt;/div&gt;
        &lt;div class=&quot;submit-form&quot;&gt;
            &lt;input type=&quot;submit&quot; value=&quot;GO&quot;&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/form&gt;
</code></pre>
<p>And there is her controller:</p>
<pre><code>&lt;?php
namespace modules\families\controllers;

use Craft;
use craft\web\Controller;
use craft\elements\Asset;
use craft\helpers\Assets;
use craft\web\UploadedFile;
use craft\elements\Entry;

class FamiliesController extends Controller
{

    protected array|bool|int $allowAnonymous = true;

    public function actionStart() {
        
        $host = Craft::$app-&gt;request-&gt;hostInfo;

        $name = $this-&gt;request-&gt;getBodyParam('name');

        $entryes = Entry::find()-&gt;section('familySeason')-&gt;phoneFamily($phone)-&gt;anyStatus()-&gt;all();

        if(count($entryes) == 0){
            $uploadedFile = UploadedFile::getInstanceByName('img');
            $tempPath = $uploadedFile-&gt;saveAsTempFile();
            $volume = Craft::$app-&gt;getVolumes()-&gt;getVolumeByHandle('uploads');
            $folder = Craft::$app-&gt;getAssets()-&gt;ensureFolderByFullPathAndVolume('family-photos', $volume, false);
            $asset = new Asset();
            $asset-&gt;newFolderId = $folder-&gt;id;
            $asset-&gt;volumeId = $volume-&gt;id;
            $asset-&gt;tempFilePath = $tempPath;
            $asset-&gt;setFilename(Assets::prepareAssetName($uploadedFile-&gt;name)); 
            $asset-&gt;avoidFilenameConflicts = true;
            $asset-&gt;setScenario(Asset::SCENARIO_CREATE);
            if(($uploadedFile-&gt;size &gt; 10000000) || ($uploadedFile-&gt;type != 'image/jpeg') &amp;&amp; ($uploadedFile-&gt;type != 'image/png')){
                $redirectUrl = $host.&quot;/family/error&quot;;
            }else{
                $success = Craft::$app-&gt;elements-&gt;saveElement($asset);
                if($success){
                    $family = new Entry();
                    $family-&gt;sectionId = 7;
                    $family-&gt;enabled = false;
                    $family-&gt;title = $name;
                    $family-&gt;setFieldValue('imageFamily', [$asset-&gt;id]);
                    $success = Craft::$app-&gt;elements-&gt;saveElement($family);
                    $redirectUrl = $host.&quot;/family/lucky&quot;;
                }
            }
        }else{
            $redirectUrl = $host.&quot;/family/error&quot;;
        }
        Craft::$app-&gt;getResponse()-&gt;redirect($redirectUrl);
    }
}
</code></pre>
<p>How can I make sure that the user has the opportunity to add several photos and not just one? Is it possible to do this with a single input or is it necessary to produce several? What needs to be tweaked in the form and in the controller to make it work?</p>
<p>What i try to do:</p>
<pre><code>$files = array();
$status = true;

foreach(UploadedFile::getInstancesByName('img') as $uploadedFile){
    $tempPath = $uploadedFile-&gt;saveAsTempFile();
    array_push($files,  $uploadedFile);
    $volume = Craft::$app-&gt;getVolumes()-&gt;getVolumeByHandle('uploads');
    $folder = Craft::$app-&gt;getAssets()-&gt;ensureFolderByFullPathAndVolume('family-photos', $volume, false);
    if(($uploadedFile-&gt;size &gt; 10000000) || ($uploadedFile-&gt;type != 'image/jpeg') &amp;&amp; ($uploadedFile-&gt;type != 'image/png')){
        $status = false;
    }
    array_push($files, $uploadedFile);
}

if($status){
    $ids = array();
    foreach($files as $uploadedFile){
        $asset = new Asset();
        $asset-&gt;newFolderId = $folder-&gt;id;
        $asset-&gt;volumeId = $volume-&gt;id;
        $asset-&gt;tempFilePath = $tempPath;
        $asset-&gt;setFilename(Assets::prepareAssetName($uploadedFile-&gt;name)); 
        $asset-&gt;avoidFilenameConflicts = true;
        $asset-&gt;setScenario(Asset::SCENARIO_CREATE);
        $success = Craft::$app-&gt;elements-&gt;saveElement($asset);
        array_push($ids,  $asset-&gt;id);
    }
    $family = new Entry();
    $family-&gt;sectionId = 7;
    $family-&gt;enabled = false;
    $family-&gt;title = $name;
    $family-&gt;setFieldValue('phoneFamily', $phone);
    $family-&gt;setFieldValue('imageFamily', [$asset-&gt;id]);
    $success = Craft::$app-&gt;elements-&gt;saveElement($family);
    $redirectUrl = $host.&quot;/family/lucky&quot;;
}else{
    $redirectUrl = $host.&quot;/family/error&quot;;
}
Craft::$app-&gt;getResponse()-&gt;redirect($redirectUrl);
</code></pre>
<p>I tried to replace UploadedFile::getInstanceByName('img') with UploadedFile::getInstancesByName('img').
And the first problem I encountered is that the UploadedFile::getInstancesByName('img') construct returns me an array, but always with one element.
My input looks like this:</p>
<pre><code>&lt;input id=&quot;file-upload&quot; type=&quot;file&quot; name=&quot;img&quot; multiple=&quot;multiple&quot; /&gt;
</code></pre>
<p>I select several files, but there is always one picture in UploadedFile::getInstancesByName('img')! Perhaps this is the reason why my code is not working properly. Can someone tell me what I'm doing wrong?! Thank you all in advance)</p>

<craft3><entries><files>
How can I add multiple files to one field through the form?
# How can I add multiple files to one field through the form?

(Asked by: username_0 on 2024-07-26)

I need help setting up a form in CRAFT CMS. There is a form:

    {{ csrfInput() }}
    {{ actionInput('families/families/start') }}
    
        
            
        
        
            
        
        
            
        
    


And there is her controller:
request->hostInfo;

        $name = $this->request->getBodyParam('name');

        $entryes = Entry::find()->section('familySeason')->phoneFamily($phone)->anyStatus()->all();

        if(count($entryes) == 0){
            $uploadedFile = UploadedFile::getInstanceByName('img');
            $tempPath = $uploadedFile->saveAsTempFile();
            $volume = Craft::$app->getVolumes()->getVolumeByHandle('uploads');
            $folder = Craft::$app->getAssets()->ensureFolderByFullPathAndVolume('family-photos', $volume, false);
            $asset = new Asset();
            $asset->newFolderId = $folder->id;
            $asset->volumeId = $volume->id;
            $asset->tempFilePath = $tempPath;
            $asset->setFilename(Assets::prepareAssetName($uploadedFile->name)); 
            $asset->avoidFilenameConflicts = true;
            $asset->setScenario(Asset::SCENARIO_CREATE);
            if(($uploadedFile->size > 10000000) || ($uploadedFile->type != 'image/jpeg') && ($uploadedFile->type != 'image/png')){
                $redirectUrl = $host."/family/error";
            }else{
                $success = Craft::$app->elements->saveElement($asset);
                if($success){
                    $family = new Entry();
                    $family->sectionId = 7;
                    $family->enabled = false;
                    $family->title = $name;
                    $family->setFieldValue('imageFamily', [$asset->id]);
                    $success = Craft::$app->elements->saveElement($family);
                    $redirectUrl = $host."/family/lucky";
                }
            }
        }else{
            $redirectUrl = $host."/family/error";
        }
        Craft::$app->getResponse()->redirect($redirectUrl);
    }
}

How can I make sure that the user has the opportunity to add several photos and not just one? Is it possible to do this with a single input or is it necessary to produce several? What needs to be tweaked in the form and in the controller to make it work?
What i try to do:
$files = array();
$status = true;

foreach(UploadedFile::getInstancesByName('img') as $uploadedFile){
    $tempPath = $uploadedFile->saveAsTempFile();
    array_push($files,  $uploadedFile);
    $volume = Craft::$app->getVolumes()->getVolumeByHandle('uploads');
    $folder = Craft::$app->getAssets()->ensureFolderByFullPathAndVolume('family-photos', $volume, false);
    if(($uploadedFile->size > 10000000) || ($uploadedFile->type != 'image/jpeg') && ($uploadedFile->type != 'image/png')){
        $status = false;
    }
    array_push($files, $uploadedFile);
}

if($status){
    $ids = array();
    foreach($files as $uploadedFile){
        $asset = new Asset();
        $asset->newFolderId = $folder->id;
        $asset->volumeId = $volume->id;
        $asset->tempFilePath = $tempPath;
        $asset->setFilename(Assets::prepareAssetName($uploadedFile->name)); 
        $asset->avoidFilenameConflicts = true;
        $asset->setScenario(Asset::SCENARIO_CREATE);
        $success = Craft::$app->elements->saveElement($asset);
        array_push($ids,  $asset->id);
    }
    $family = new Entry();
    $family->sectionId = 7;
    $family->enabled = false;
    $family->title = $name;
    $family->setFieldValue('phoneFamily', $phone);
    $family->setFieldValue('imageFamily', [$asset->id]);
    $success = Craft::$app->elements->saveElement($family);
    $redirectUrl = $host."/family/lucky";
}else{
    $redirectUrl = $host."/family/error";
}
Craft::$app->getResponse()->redirect($redirectUrl);

I tried to replace UploadedFile::getInstanceByName('img') with UploadedFile::getInstancesByName('img').
And the first problem I encountered is that the UploadedFile::getInstancesByName('img') construct returns me an array, but always with one element.
My input looks like this:


I select several files, but there is always one picture in UploadedFile::getInstancesByName('img')! Perhaps this is the reason why my code is not working properly. Can someone tell me what I'm doing wrong?! Thank you all in advance)

**Tags:** craft3, entries, files


---

## 1 answers

### Answer by username_1 on 2024-08-16. Score: 1

Have you tried naming your field `img[]`? So something like this...

There's a bit more info on this answer [here](https://stackoverflow.com/a/12006515/1809628). That should hopefully get you to where you need to be.


---

