/** * Example of AJAX File Upload with Session Progress and partial validation. * * @return array|ViewModel */ public function sessionPartialAction() { $form = new Form\SingleUpload('file-form'); $inputFilter = $form->getInputFilter(); $container = new Container('partialExample'); $tempFile = $container->partialTempFile; if ($this->getRequest()->isPost()) { // POST Request: Process form $postData = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); // Disable required file input if we already have an upload if (isset($tempFile)) { $inputFilter->get('file')->setRequired(false); } $form->setData($postData); if ($form->isValid()) { // If we did not get a new file upload this time around, use the temp file $data = $form->getData(); if (empty($data['file']) || isset($data['file']['error']) && $data['file']['error'] !== UPLOAD_ERR_OK) { $data['file'] = $tempFile; } // // ...Save the form... // if (!empty($postData['isAjax'])) { // Send back success information via JSON $this->sessionContainer->formData = $data; return new JsonModel(array('status' => true, 'redirect' => $this->url()->fromRoute('fileupload/success'), 'formData' => $data)); } else { // Non-JS form submit, redirect to success page return $this->redirectToSuccessPage($data); } } else { // Extend the session $container->setExpirationHops(1, 'partialTempFile'); // Form was not valid, but the file input might be... // Save file to a temporary file if valid. $data = $form->getData(); $fileErrors = $form->get('file')->getMessages(); if (empty($fileErrors) && isset($data['file']['error']) && $data['file']['error'] === UPLOAD_ERR_OK) { // NOTE: $data['file'] contains the filtered file path. // 'FileRenameUpload' Filter has been run, and moved the file. $container->partialTempFile = $tempFile = $data['file']; } if (!empty($postData['isAjax'])) { // Send back failure information via JSON return new JsonModel(array('status' => false, 'formErrors' => $form->getMessages(), 'formData' => $data, 'tempFile' => $tempFile)); } } } else { // GET Request: Clear previous temp file from session unset($container->partialTempFile); $tempFile = null; } return array('title' => 'Session Partial Progress Upload', 'form' => $form, 'tempFiles' => isset($tempFile) ? array($tempFile) : null); }
/** * Example of a single file upload when form is partially valid. * * @return array|ViewModel */ public function partialAction() { $form = new Form\SingleUpload('file-form'); $inputFilter = $form->getInputFilter(); $container = new Container('partialExample'); $tempFile = $container->partialTempFile; if ($this->getRequest()->isPost()) { // POST Request: Process form $data = array_merge($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); // Disable required file input if we already have an upload if (isset($tempFile)) { $inputFilter->get('file')->setRequired(false); } $form->setData($data); if ($form->isValid()) { // If we did not get a new file upload this time around, use the temp file $data = $form->getData(); if (empty($data['file'])) { $data['file'] = $tempFile['tmp_name']; } // // ...Save the form... // return $this->redirectToSuccessPage($data); } else { // Extend the session $container->setExpirationHops(1, 'partialTempFile'); // Form was not valid, but the file input might be... // Save file to a temporary file if valid. $data = $form->getData(); if (!empty($data['file'])) { // NOTE: $data['file'] contains the filtered file path $tempFile = $form->get('file')->getValue(); // Get the raw file upload array value $tempFilePath = './data/tmpuploads/partial' . uniqid('_'); move_uploaded_file($data['file'], $tempFilePath); $tempFile['tmp_name'] = $tempFilePath; $container->partialTempFile = $tempFile; } } } else { // GET Request: Clear previous temp file from session unset($container->partialTempFile); $tempFile = null; } $view = new ViewModel(array('title' => 'Partial Validation Examples', 'form' => $form, 'tempFiles' => isset($tempFile) ? array($tempFile) : null)); $view->setTemplate('zf2-file-upload-examples/examples/single'); return $view; }
/** * Example of a single file upload when form is partially valid. * * @return array|ViewModel */ public function partialAction() { $form = new Form\SingleUpload('file-form'); $inputFilter = $form->getInputFilter(); $container = new Container('partialExample'); $tempFile = $container->partialTempFile; if ($this->getRequest()->isPost()) { // POST Request: Process form $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()); // Disable required file input if we already have an upload if (isset($tempFile)) { $inputFilter->get('file')->setRequired(false); } $form->setData($data); if ($form->isValid()) { // If we did not get a new file upload this time around, use the temp file $data = $form->getData(); if (isset($data['file']['error']) && $data['file']['error'] !== UPLOAD_ERR_OK) { $data['file'] = $tempFile; } // // ...Save the form... // return $this->redirectToSuccessPage($data); } else { // Extend the session $container->setExpirationHops(1, 'partialTempFile'); // Form was not valid, but the file input might be... // Save file to a temporary file if valid. $data = $form->getData(); $fileErrors = $form->get('file')->getMessages(); if (empty($fileErrors) && isset($data['file']['error']) && $data['file']['error'] === UPLOAD_ERR_OK) { // NOTE: $data['file'] contains the filtered file path. // 'FileRenameUpload' Filter has been run, and moved the file. $container->partialTempFile = $tempFile = $data['file']; } } } else { // GET Request: Clear previous temp file from session unset($container->partialTempFile); $tempFile = null; } $view = new ViewModel(array('title' => 'Partial Validation Examples', 'form' => $form, 'tempFiles' => isset($tempFile) ? array($tempFile) : null)); $view->setTemplate('zf2-file-upload-examples/examples/single'); return $view; }