Duplicating Single Upload To Save With Different Models

Hi,

I have a checkboxlist populated by values from a database search entry.

I am trying to upload a file and save it as separate DB entries based on the checkbox values input by the user.

Here is what I am trying to achieve.

  1. Upload a single file.

  2. Save file under different filenames based on checkbox inputs

Current result: Models are saved into DB but only one copy of the uploaded file is saved into the server.

I have tried to store copies of the uploaded file in an array to reuse it during saving of each model but this has not yielded any result as well.

Here is my code.





public function actionProcessing(){


                //Find models to populate checkboxList (no issue)

		$modelClaims = ClaimAmount::model()->with('hasAnnletter')->findAll('status!=(5||6||7)');

		$modelFiles	 = new Files;

		$number = count($modelClaims);


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

			$prf[] 			= $modelClaims[$i]->prf;

			$bp[]	 			= $modelClaims[$i]->bp;

			$combined[]		= $prf[$i].' | '.$bp[$i];

		}


		$combined = array_combine(array_values($combined),$combined);


	if(!empty($_POST['Files'])){

		//Saving Files

		$prfList = $_POST['prfList'];

		$count 	= count($prfList)*2; // Number of sets multiplied by 2 due to split below

		$prfList = implode(',',$prfList); // Split into string sets

		$prfList	= preg_split('/[,|]/',$prfList); // Split into array of PRF and BP in even and odd sets

		//Storing copies of file in array

                $j=0;

		while($j<$count){

		$uploadedFile[]				= CUploadedFile::getInstance($modelFiles,'image'); 

		$j++;

		}

      

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

           

       $modelFiles->attributes 	= $_POST['Files'];

       $modelFiles->bp 			= $prfList[$i+1];

       $modelFiles->file_prf 		= $prfList[$i];

       $modelPrf			= Prf::model()->findbyPk($modelFiles->file_prf);

       $modelFiles->country 		= $modelPrf->bp_country;      

       $fileName 			= $prfList[$i].'_'.$uploadedFile[$i];                       

       $modelFiles->image 		= $fileName;

       $modelFiles->id_user 		= Yii::app()->user->name;

       $modelFiles->file_name           = $modelFiles->image;

		 


		 

         

       if($modelFiles->save())

            {

            	//Save as PRFnumberFilename

               $uploadedFile[$i]->saveAs(Yii::app()->basePath.'/../images/upload/'.$fileName);

		 			$modelFiles	= new Files;


            }

         $i = $i+1;

        }     

      

		

		$this->refresh(); 


		

		}




		$this->render('processing',array('modelClaims'=>$modelClaims,

'modelFiles'=>$modelFiles,

															'combined'=>$combined,

															'prf'=>$prf,

															'bp'=>$bp));

	}



Question is, how do I duplicate the file and save them as different filenames based on user input?

to duplicate the file upload first of all you should save the uploaded file to runtime folder and then copy that file to various folders with various names and then finally delete the image from runtime folder thats it.

Wouldn’t it work to just re-save it numerous time?




$uploadedFile = CUploadedFile::getInstance($modelFiles,'image'); 


if($modelFiles->save()) {

   //Save as PRFnumberFilename


   foreach (xxxx as $filename)

      $uploadedFile->saveAs(Yii::app()->basePath.'/../images/upload/'.$fileName);

   }



Another question is: Why save numerous times? How many copies of the same file do you really need? Wouldn’t be better to same the “Yii::app()->basePath.’/../images/upload/’.$filename” to the different models and just reuse to file?

I know that storage is getting cheaper, but saving numerous copies of a file for website use just wrong? As a backup copy fine, but active usage???????

Hi,

Jkofsky, you are actually right in that sense. I was thinking of that solution when I moved away from the computer because as you said it is cheaper and logically speaking makes more sense!

I might just be thinking too much at times that the simpler solution escapes me

Nevertheless thanks for your inputs cos it surely helps!

No Prob…

As programmers we sometimes over think it.I know I do ;D with 6 different ways to do the same thing…go figure.

One thing I always try to do is to relate what I am doing to something similar.

for example I made a Podcast site…Oh wait it’s just a blog system with an added field to store the podcast.