如何在Zend Framework 2和Zend Filter中压缩Zip格式的上传文件

时间:2014-02-24 07:45:48

标签: php file-upload zend-framework2 zend-filter

如何使用\Zend\File\Transfer\Adapter\Http()\Zend\Filter\Compress()上传文件并对其进行压缩?

这是我已经写过的:

public function UploadprocessAction()
{
    $this->layout('layout/myaccount');

    // Get the identity information
    $Identity = $this->zfcUserAuthentication()->getIdentity();
    $userId = $Identity->getId();

    // Get the TableGateway object to retrieve the data
    $user = $this->getServiceLocator()->get('PdTable');

    // Get the user
    $myUser = $user->getPdByUserId($userId);

    $form = $this->getServiceLocator()->get('FormElementManager')->get('Cv\Form\UploadForm');

    $request = $this->getRequest();

    if ($request->isPost()) {
        $upload = new Uploads();
        $uploadFile    = $this->params()->fromFiles('fileupload');
        $uploadPath = $this->getFileUploadLocation();

        $form->setData($request->getPost());

        if ($form->isValid()) {

            $codedfile = strtolower($myUser->getPac() . "_" . $this->CreateCode()) . ".zip";

            // Fetch Configuration from Module Config
            $uploadPath    = $this->getFileUploadLocation();

            // Save Uploaded file
            $adapter = new \Zend\File\Transfer\Adapter\Http();
            $adapter->setDestination($uploadPath);

            // Do not allow files with extension php or exe
            $adapter->addValidator('ExcludeExtension', false, 'php,exe');

            // Adds a filter to lowercase the uploaded textfile
            $adapter->addFilter('LowerCase');

            $filter = new Compress(array(
                    'adapter' => 'Zip',
                    'options' => array (
                            'target' => $uploadPath . "/" .$codedfile,
                            'archive' => $codedfile
                    )
            ));

            $adapter->addFilter($filter);

            if ($adapter->receive()) {

                $exchange_data = array();
                $exchange_data['title'] = $request->getPost()->get('title');
                $exchange_data['originalfilename'] = $uploadFile['name'];
                $exchange_data['codedfilename'] = $codedfile;
                $exchange_data['mimetype'] = $uploadFile['type'];
                $exchange_data['size'] = $uploadFile['size'];
                $exchange_data['idpersonaldata'] = $myUser->id;

                $upload->exchangeArray($exchange_data);

                $uploadTable = $this->getServiceLocator()->get('UploadsTable');
                $uploadTable->saveUpload($upload);

            }

            return $this->redirect()->toRoute('cv/uploads' , array(
                    'action' =>  'index'
            ));
        }
    }

    $viewModel = new ViewModel(array (
            'error' => true,
            'form' => $form
    ));
    $viewModel->setTemplate('cv/uploads/upload');
    return $viewModel;

}

但是通过这种方式,它无需任何压缩即可上传文件。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

您必须先接收文件,然后才能调用压缩过滤器。