Hi,

I am working on a project using zend framework and i am stuck with a
problem. I have to upload three files via a single form. The files must be
mp3. Only one of them is compulsary . I was able to do study about it but
still few bugs in my code.


If i upload three files with same name then only one is uploaded as the
portion where i recieve the files is working using name of file and i think
recieveing all the files as the name is same for them.

Also i am not able to make the first upload file compulsary .


Below is th code i have wrriten.



===============   Controller =====================



$form = new MusicUploadForm();
        $form->get('submit')->setAttribute('value', 'Upload');
 $request = $this->getRequest();
        if ($request->isPost()) {
 //preparing form and setting validatores
            $musicupload = new Musics();
$form->setInputFilter($musicupload->getInputFilter());
 //fetching the data files and non files
 $nonFile = $request->getPost()->toArray();
$Files = $request->getFiles()->toArray();
//checking for form validation
 $data = array_merge(
                 $nonFile,
                 $Files
             );
 $form->setData($data);
  if ($form->isValid()) {

  //setting the validators, adapters and filters
 $adapter = new \Zend\File\Transfer\Adapter\Http();
$validator = new \Zend\Validator\File\MimeType('audio/mpeg');
 $filter = new \Zend\Filter\File\Rename(array(
"target"    => "public/songs/song.mp3",
"randomize" => true,
));
  //applying validators
 foreach($Files as $key1 => $File){
 if((int)$File['error'] == 0){
 $adapter->setValidators(array($validator), $File['name']);
if (!$adapter->isValid($File['name'])){
$dataError = $adapter->getMessages();
$error = array();
foreach($dataError as $key=>$row)
{
$error[] = $row;
}
 $form->setMessages(array( $key1 => $error ));
}
 }

$adapter->clearValidators();
}

 if(!isset($error)){
 $upload->setDestination('public/songs');
 // saving to db
 foreach($Files as $key => $File){
 if ($adapter->receive($File['name'])) {
 $newName = $filter->filter($adapter->getFileName($File['name']));
$record = array();
$record['org_name'] = $File['name'];
$record['temp_name'] = $newName;
$record['first_name'] = $data['first_name'];
$record['last_name'] = $data['last_name'];
$this->getMusicsTable()->addMusic($record);
 }
}
 $this->flashMessenger()->addSuccessMessage('Songs successfully uploaded.');
return $this->redirect()->toRoute('listmusic');
 }
 }
 }







===========filter for input type file ===================


$inputFilter->add(
                $factory->createInput(array(
                    'name'     => 'song_1',
                    'required' => true,
                ))
            );




I need this done very quickly. Any kind of help is much much appriciated.






Thanks

Reply via email to