Yii Framework Forum: Duplicating Single Upload To Save With Different Models - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Duplicating Single Upload To Save With Different Models Rate Topic: -----

#1 User is offline   m3rck 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 03-March 14

Posted 16 April 2014 - 05:05 AM

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?
0

#2 User is offline   Ahamed Rifaideen 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 384
  • Joined: 14-February 13
  • Location:chennai, tamilnadu

Posted 16 April 2014 - 10:04 AM

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.
My Blog - Yii Learner
0

#3 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 537
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 16 April 2014 - 01:20 PM

View Postm3rck, on 16 April 2014 - 05:05 AM, said:

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?

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???????
Do, or do not. There is no 'try.' Jedi Master Yoda
1

#4 User is offline   m3rck 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 03-March 14

Posted 16 April 2014 - 07:40 PM

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!
0

#5 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 537
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 16 April 2014 - 07:53 PM

View Postm3rck, on 16 April 2014 - 07:40 PM, said:

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.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users