Jump to content

Phalcon php Saving record plus uploads multiple files


jkkenzie

Recommended Posts

Hi:

Am not sure if this is the best algorithm to go about  saving a record and uploading (plus moving to another directory) files. I feel i need another algorithm?

if (self::post()->isPost()) { 
                           
            #check if there is any file
            if($this->request->hasFiles() == true){            
                    $uploads = $this->request->getUploadedFiles();
                    $isUploaded = false;
                    #do a loop to handle each file individually
                    foreach($uploads as $upload){
                        #define a "unique" name and a path to where our file must go
                        $path = 'temp/'.md5(uniqid(rand(), true)).'-'.strtolower($upload->getname());

                        #move the file and simultaneously check if everything was ok
                       if($upload->moveTo($path)) {                          
                        #Build an array to be sent to the API for moving the uploaded file to front end directory                        
                           $files="";
                           $files = array(
                            'name' => $upload->getName(),
                            'link' => $this->di['url']->get('temp/' . $upload->getName()),
                            'direction' => 'adverts'
                            );
                        
                          $saveResult = self::request()->request('move-advert/', array('file' => $files), 'POST');
                          if($saveResult->response == 'success') $isUploaded = true; else $isUploaded = false;                          
                           
                       }
                    }
                    #if any file couldn't be moved, then throw an message
                    if($isUploaded) {                         
                        
                        $save_response = self::request()->request('advert/' . $id, self::editValues(self::post()->getPost()), 'POST');
                        if ($save_response->response == 'success') 
                            {
                                 $this->flashSession->success('Advert Updated');
                                 return $this->response->redirect('adverts/list-adverts');
                            } else {
                                $this->flashSession->notice('Could not update Advert');
                            }                                             
                        
                    } else {
                            $this->flashSession->notice('Some error ocurred. Please try again');
                   }
             
             }else {            
                 $this->flashSession->notice('You must Choose an advert image');
            }    
        }

Any suggestions?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.