In this section we will create a new document management interface. The document management interface will allow users to upload documents, manage uploads, and share uploaded documents with other users. The user interface will also allow users to manage sharing, and add/remove shares.
In this section, we will focus on providing users with options to create file uploads and manage those uploads. We will be using the filesystem to store the uploaded file and the relative path of the uploaded file will be stored in the database mapped to the user who uploaded the file.
Some of the important Zend Framework components used in file uploads are:
◆ File upload form element (Zend\Form\Element\File): The File upload element is used in the upload form to display a file input box. This element is an equivalent of the <input type='file'../> style element in HTML used for allowing users to upload files. The file input element can be rendered by setting 'type' => 'file' in the form definition.
◆ File transfer adapter (Zend\File\Transfer\Adapter\Http): The file transfer adapter handle file uploads upon form submission. The setDestination() method in the file transfer adapter allows the user to set a destination and receive the file in that destination. The receive() method is used to initiate the transfer.
In this task, we will be creating a new document upload form; file uploads will be stored in the filesystem, and the information regarding the file upload will be stored in the database in a table named uploads. The file uploads are stored in a folder location defined in the module configuration. Perform the following steps to do so:
1. Our first step will be to define a location where files can be uploaded in the module's configuration (config/module.config.php):
<?php
return array(
// Other configurations
// ..
// ..
// MODULE CONFIGURATIONS 'module_config' => array(
'upload_location' => DIR
),
);
. '/../data/uploads',
CREATE TABLE IF NOT EXISTS uploads (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , filename VARCHAR( 255 ) NOT NULL ,
label VARCHAR( 255 ) NOT NULL , user_id INT NOT NULL,
UNIQUE KEY (filename)
);
3. Create the Upload and UploadTable classes for interacting with the uploads table. Add default methods such as saveUpload(), fetchAll(), getUpload(), and deleteUpload(). Also, add a method toget uploads made by a specific user getUploadsByUserId($userId):
public function getUploadsByUserId($userId)
{
$userId = (int) $userId;
$rowset = $this->tableGateway->select( array('user_id' => $userId));
return $rowset;
}
4. Create an UploadManagerController controller for managing file uploads. Add
indexAction() to display the list of uploads done by the user:
$uploadTable = $this->getServiceLocator()
->get('UploadTable');
$userTable = $this->getServiceLocator()
->get('UserTable');
// Get User Info from Session
$userEmail = $this->getAuthService()
->getStorage()->read();
$user = $userTable->getUserByEmail($userEmail);
$viewModel = new ViewModel( array(
'myUploads' => $uploadTable->getUploadsByUserId($user->id),
));
return $viewModel;
$this->add(array(
'name' => 'fileupload', 'attributes' => array(
'type' => 'file',
),
'options' => array(
'label' => 'File Upload',
),
));
6. Create views for the file upload form, and the index action. Now we have all the necessary elements to handle a file upload. We need to read the configuration for the file upload path and use the Zend HTTP filetransfer adapter to receive the file
in the configuration location. The get('config') method on the service locator is used to retrieve the configuration. The following code is used to read the file upload location from the configuration:
public function getFileUploadLocation()
{
// Fetch Configuration from Module Config
$config = $this->getServiceLocator()->get('config'); return $config['module_config']['upload_location'];
}
7. The last step is to handle the file upload process. There are two actions that need to happen once the form is successfully submitted:
1. The uploaded file has to be moved to the file upload locations.
2. An entry needs to be added describing the upload in the 'uploads' table
using the following code:
$uploadFile = $this->params()->fromFiles('fileupload');
$form->setData($request->getPost());
if ($form->isValid()) {
// Fetch Configuration from Module Config
$uploadPath = $this->getFileUploadLocation();
// Save Uploaded file
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination($uploadPath);
if ($adapter->receive($uploadFile['name'])) {
// File upload sucessfull
$exchange_data = array();
$exchange_data['label'] = $request->getPost()
->get('label');
$exchange_data['filename'] = $uploadFile['name'];
$exchange_data['user_id'] = $user->id;
$upload->exchangeArray($exchange_data);
$uploadTable = $this->getServiceLocator()
->get('UploadTable');
$uploadTable->saveUpload($upload);
return $this->redirect()
->toRoute('users/upload-manager' , array('action' => 'index'
));
}
}
8. Add a child route (upload manger) for the UploadManager controller and the controller to the invokables list.
9. Open the web browser and test the upload form.
The final form will look like the following screenshot:
Không có nhận xét nào:
Đăng nhận xét