Uploading multiple files at once?

Hi all,

I am currently developing an upload feature for my application. I want to create photo album (no problem) and populate these albums with a form like this:

[DropdownAlbum]

[FileuploadField]

For the upload I use Plupload (http://www.plupload.com) which lets me upload several images at once. Here is my Photo.php:




/* The form gets submitted after all files have been uploaded */

public function actionCreate()

	{

		$model=new Photo;


		if(isset($_POST['Photo']))

		{

            // this variable contains how many images were uploaded

            $length = (int) $_POST['counter_upload']; 


            for ($i = 0; $i < $length; $i++) {

                $model = new Photo;

                $model->path = $_POST;

                $model->attributes = $_POST['Photo'];

                $model->save();

            }

			$this->redirect(array('index'));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}


    /* This action is called for every single file that gets uploaded */

    public function actionUpload() {

        $uploadedFile = CUploadedFile::getInstanceByName('file');

        $uploadedFile->saveAs('photos' . DIRECTORY_SEPARATOR . $uploadedFile->getName());

    }



So this is what happens if I want to add 2 Pictures to an album:

  1. actionCreate() - Show form

  2. Fill in Album name + select two files to upload

  3. actionUpload() for file1 is called

  4. actionUpload() for file2 is called

  5. actionCreate() is called an $_POST is filled

My problem is that I somehow need to communicate between these 2 methods. I have actionUpload() which only has access to $_FILES and actionCreate() which only has access to $_POST, but I need both in order to set the correct album for each of the photos.

Anyone has ideas how to solve this without writing into temp files or something like that?

Okay, I figured it out myself. I just leave the files with their unique temporary file names at upload time. When the form is sent and actionCreate() is called again, I have 3 variables for each file:

[list=1]

[*]uploader_0_status

[*]uploder_0_name

[*]uploader_0_tmpname

[/list]

So this is what my actionCreate() looks now:




public function actionCreate()

	{

		$model=new Photo;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

		if(isset($_POST['Photo']))

		{

            $length = (int) $_POST['uploader_count'];


            for ($i = 0; $i < $length; $i++) {

                /* Uploaded files will have set the status to "done" when completed */

                $status = $_POST['uploader_' . $i . '_status'];

                if ($status !== 'done')

                    continue;




                $model = new Photo;

                $model->attributes = $_POST['Photo'];


                $tmpName = 'photos' . DIRECTORY_SEPARATOR . $_POST['uploader_' . $i . '_tmpname'];

                $ext = strtolower(pathinfo($tmpName, PATHINFO_EXTENSION));

                $newName = 'photos' . DIRECTORY_SEPARATOR . uniqid('photo_') . '.' . $ext;


                if (rename($tmpName, $newName)) {

                    $model->path = $newName;

                    $model->save();

                }

            }


			$this->redirect(array('index'));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}



actionUpload() is not used that way, I can use the upload.php that comes together with Plupload.

Hi Tropi,

Could you elaborate a little more on what you have done? I need to accomplish something similar and I am scratching my head. A few questions:

  • Is your

actionUpload()

a callback function? If so, how does it get called?

  • Could you post your Photo model?

Thanks!

hi, tropi.

i have to upload multiple pictures related to a property. ‘picture’ table will store the path of the picture (let say ‘/../images’) and a foreign key of ‘property_id’. would u like to help me in model, view, and controller. how to implement this process.

Regards.