Cakephp : how can i retrieve my media files from the folder to display on my view page -


i working on cakephp 2.x .. have done right .. displaying user form in have given him option upload audio file .. have taken file user .. saving file app/uploads folder .. , path database ... problem dont how can know retrieve audio file , show them view page

here uploading function

 public function audio(){       if ($this->request->ispost()){         $this->loadmodel('audio');         $file = $this->request->data['audio']['file'];           $iduser = $this->auth->user('iduser');           if ($file['error'] === upload_err_ok) {             $id = string::uuid();             $name =$file['name'];              $folder_url = app.'uploads/'.$iduser;         if(!is_dir($folder_url)) {             mkdir($folder_url);         }         move_uploaded_file($file['tmp_name'],  $folder_url.ds.$name);              $this->request->data['audio']['user_id'] = $iduser;             $this->request->data['audio']['filename'] = $file['name'];             $this->request->data['audio']['filesize'] = $file['size'];             $this->request->data['audio']['filemime'] = $file['type'];             $this->audio->save($this->request->data);               return true;         }     }     return false;   }   public function showallaudiofiles(){    } 

now file of particular user has stored folder app/uploads/23/file.mp3

answer:

assuming have 1 file per user, jsut need find database record , construct path that:

public function showallaudiofiles(){     $record = $this->audio->find('first', array('conditions' => array('user_id' => $this->auth->user('iduser')));     $file = app . 'uploads' . ds . $this->auth->user('iduser') . ds . $record['audio']['filename'] . '.' . $record['audio']['filemime'];     $this->set('file', $file); } 

if user can have multiple files:

public function showallaudiofiles(){     $records = $this->audio->find('all', array('conditions' => array('user_id' => $this->auth->user('iduser')));     $files = array();     foreach ($records $record) {         $files[] = app . 'uploads' . ds . $this->auth->user('iduser') . ds . $record['audio']['filename'] . '.' . $record['audio']['filemime'];     }     $this->set('files', $files); } 

now $file variable contain string of format app/uploads/23/file.mp3, example. or $files array of strings.

suggestion:

doing proper 'cake' way, retrieve records , echo them using media method of htmlhelper:

action:

public function showallaudiofiles(){         $record = $this->audio->find('first', array('conditions' => array('user_id' => $this->auth->user('iduser')));         $this->set('file', $record); } 

view:

echo $this->html->media($file['audio']['filename'] . '.' . $file['audio']['filemime'],     array('pathprefix' => 'uploads' . ds . $file['audio']['user_id'] ); 

this should output this:

 <audio src="/uploads/23/file.mp3"></audio> 

creating link file

edit: links files, use htmlhelper's link method:

 echo $this->html->link($file['audio']['filename'], www_root . ds . 'uploads' . ds . $file['audio']['user_id'] . ds . $file['audio']['filename'] . ds . $file['audio']['filemime']); 

htmlhelper::media


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -