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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.