Two Filefields On The Same Create Form

Hi there,

I’m having a problem with uploading files. I have a create form that has two fileFields.

If I enter 1 file and save it saves fine for either field, but if I try to enter files in both and then save, it only saves the first file and gives a PHP warning that the second file was empty.

Here is my code:


//upload epc file

$epcUploadFile = CUploadedFile::getInstance($model,'epc');

if($epcUploadFile !== null) //only do this if thie file is really uploaded

{

    $epcFileName = time().$epcUploadFile->name;

    $model->epc = $epcFileName;

}

			

//upload floor plan file

$floorPlanUploadFile = CUploadedFile::getInstance($model,'floor_plan');

if($floorPlanUploadFile !== null) //only do this if thie file is really uploaded

{

    $floorPlanFileName = time().$floorPlanUploadFile->name;

    $model->floor_plan = $floorPlanFileName;

}

			

if($model->save()) {

				

    //save the epc file if present

    if($epcUploadFile !== null)

    {

        $epcUploadFile->saveAs('../myPath/'.$epcFileName);

    }

				

    //save the floor plan file if present

    if($floorPlanUploadFile !== null)

    {

        $floorPlanUploadFile->saveAs('../myPath/'.$floorPlanFileName);

    }

				

				

    //redirect etc

				

								

}

Can someone tell me what I need to do so I can upload both at once?

Thanks in advance.

Have you tried with very small files to make sure you’re not hitting PHP upload size limits?

Yes, and strangely if I save the form with no files and then update and add the files, I can add them both at the same time.