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

标签 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;

}

但是在这种方式下,它上传的文件没有任何压缩。我的代码有什么问题?

最佳答案

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

关于php - 如何在 Zend Framework 2 和 Zend Filter 中以 Zip 格式压缩上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21981744/

相关文章:

php - Zend 框架 2 中的 Zend\ServiceManager\Exception\ServiceNotFoundException

php - Zend Framework 2 - 删除的表单元素导致验证失败

php - 正则表达式 If Else

php - Zend Server CE,卸载后,localhost 不起作用

php - Zend Framework 2 获取多行

python - 使用 Django 处理上传的文件

python - 每次使用不同的文件夹以特定路径在 Django 数据库中上传文件

php函数生成随机字符串,连续返回重复值

php - 网络游戏如何将玩家联系在一起?

带文件上传功能的 JQuery Ajax 表单在 IE 中不起作用