<form id="uploadpdf_form" class="zdk-form">
<fieldset>
<legend>Upload</legend>
<label>PDF file</label>
<input type="file" name="mypdffile" data-zdk-action="pdfcontroller:upload" required>
</fieldset>
<button class="zdk-bt-upload" type="submit">Upload</button>
</form>
<form id="downloadpdf_form" class="zdk-form">
<fieldset>
<legend>Download</legend>
<ul></ul>
</fieldset>
</form>
<script>
$(function () {
// No thumbnail of the selected file
$("#uploadpdf_form input[name=mypdffile]").zdkinputfile({
showThumbnail:false /* No thumbnail displayed */
});
// The hyperlink of the uploaded file is displayed for download
$("#uploadpdf_form").on('zdkformcomplete', function(context, response){
var filename = response.downloadedFile;
$('#downloadpdf_form ul').append('<li><a href="<?php echo \General::getURIforDownload('pdfcontroller','file=');?>'
+ filename + '" target="_blank">' + filename + '</a></li>');
$("#uploadpdf_form input[name=mypdffile]").zdkinputfile('reset');
});
});
</script>
<?php
namespace app\controller;
class PDFController extends \AppController {
static protected function action_upload() {
$request = new \Request();
$fileInfos = $request->getUploadedFileInfos('mypdffile');
$targetFileName = CFG_DOCUMENTS_DIR . DIRECTORY_SEPARATOR . $fileInfos['basename'];
$response = new \Response();
if (!file_exists($targetFileName)) {
move_uploaded_file($fileInfos["tmp_name"], $targetFileName);
$response->setSuccessMessage("Upload", "File '{$fileInfos['basename']}' uploaded successfully!");
$response->downloadedFile = $fileInfos['basename'];
} else {
$response->setFailedMessage("Upload", "File '{$fileInfos['basename']}' already exists!");
}
return $response;
}
static protected function action_download() {
$request = new \Request();
$storedFile = CFG_DOCUMENTS_DIR . DIRECTORY_SEPARATOR . $request->file;
$response = new \Response();
if (file_exists($storedFile)) {
$response->setFileToDownload($storedFile, TRUE);
} else {
$response->setFailedMessage("Download", "File '{$request->file}' does not exist!");
}
return $response;
}
}
<form id="upload_store_form" class="zdk-form" data-zdk-action="pdfctrl:save">
<fieldset>
<label>PDF file</label>
<input type="file" name="pdf_file" data-zdk-action="pdfctrl:upload" required>
<label>File name</label>
<input name="filename" required>
<label>Description</label>
<input name="description" required>
<legend>Upload</legend>
</fieldset>
<button class="zdk-bt-save" type="submit">Submit</button>
</form>
<form id="list_of_files_form" class="zdk-form">
<fieldset>
<legend>Download</legend>
<ul></ul>
</fieldset>
</form>
<script>
$(function () {
// Refresh the list of stored files
var refreshFileList = function() {
var uriForDownload = '<?php echo \General::getURIforDownload('pdfctrl');?>';
znetdk.request({
control:'pdfctrl',
action:'files',
callback: function(response) {
var listElement = $('#list_of_files_form ul'),
files = response;
listElement.empty();
for (i = 0; i < files.length; i++) {
$('#list_of_files_form ul').append('<li><a href="'
+ uriForDownload + '&file_id=' + files[i].id
+ '" title="' + files[i].description
+ '" target="_blank">' + files[i].filename + '</a></li>');
}
}
});
}
// The file list is loaded when the view is displayed for the 1st time
refreshFileList();
// No thumbnail displayed when a file is selected
$("#upload_store_form input[name=pdf_file]").zdkinputfile({
showThumbnail:false /* No thumbnail displayed */
});
$("#upload_store_form input[name=pdf_file]").on('change', function(){
var selectedFile = $(this).zdkinputfile('getFileName');
$("#upload_store_form input[name=filename]").val(selectedFile);
$("#upload_store_form input[name=description]").focus();
});
// The file list is refreshed only if the form is successfully submited
$("#upload_store_form").on('zdkformcomplete', function(context, response){
refreshFileList();
// The file upload form is reset in the same time...
$(this).zdkform('reset');
});
});
</script>
<?php
namespace app\controller;
class PDFCtrl extends \AppController {
static protected function action_upload() {
$request = new \Request();
$fileInfos = $request->getUploadedFileInfos('pdf_file');
// The uploaded file is stored into the './applications/default/documents'
// directory with its original name prefixed by the session ID.
$targetFileName = CFG_DOCUMENTS_DIR . DIRECTORY_SEPARATOR
. session_id() . '_' . $fileInfos['basename'];
$response = new \Response();
if (!file_exists($targetFileName)) {
move_uploaded_file($fileInfos["tmp_name"], $targetFileName);
$response->setSuccessMessage("Upload", "File '{$fileInfos['basename']}' uploaded successfully!");
$response->downloadedFile = $fileInfos['basename'];
} else {
$response->setFailedMessage("Upload", "File '{$fileInfos['basename']}' already exists!");
}
return $response;
}
static protected function action_save() {
$request = new \Request();
$response = new \Response();
$createFileInfos = $request->getValuesAsMap('filename', 'description');
// The 'pdf_file' POST parameter contains the name of the file firstly
// uploaded through the 'upload' action.
$createFileInfos['location'] = session_id() . '_' . basename($request->pdf_file);
$filesDao = new \app\model\StoredFilesDAO();
$filesDao->beginTransaction();
// The file infos are added to database
$rowId = $filesDao->store($createFileInfos, FALSE);
$filePath = CFG_DOCUMENTS_DIR . DIRECTORY_SEPARATOR;
$finalLocation = $rowId . '_' . basename($request->pdf_file);
// The uploaded file is renamed : the session ID that prefixes the name
// is replaced by the database row ID
$success = rename($filePath . $createFileInfos['location'],
$filePath . $finalLocation);
if ($success) { // The new file name is updated in the database
$updateFileInfos = array('id'=>$rowId, 'location'=>$finalLocation);
$filesDao->store($updateFileInfos, FALSE);
$filesDao->commit();
$response->setSuccessMessage('File storage',
"File '{$createFileInfos['filename']}' stored successfully.");
} else { // An error occured while renaming the file
$filesDao->rollback();
$response->setFailedMessage('File storage',
"Unable to store the file '{$createFileInfos['filename']}'.");
}
return $response;
}
static protected function action_files() {
$filesDao = new \app\model\StoredFilesDAO();
$files = array();
while($row = $filesDao->getResult()) {
$files[] = $row;
}
$response = new \Response();
// The list of stored files is returned
$response->setResponse($files);
return $response;
}
static protected function action_download() {
$request = new \Request();
$response = new \Response();
// The file is serached from its database row ID
$filesDao = new \app\model\StoredFilesDAO();
$fileInfos = $filesDao->getById($request->file_id);
if ($fileInfos === FALSE) {
$response->setFailedMessage("Download",
"File '{$request->file_id}' is unknown in the database!");
return $response;
}
// The 'location' columns contains the filename of the file stored on
// the web server
$storedFile = CFG_DOCUMENTS_DIR . DIRECTORY_SEPARATOR . $fileInfos['location'];
if (file_exists($storedFile)) {
$response->setFileToDownload($storedFile, TRUE);
} else {
$response->setFailedMessage("Download", "File '{$fileInfos['location']}' not found!");
}
return $response;
}
}
<?php
namespace app\model;
class StoredFilesDAO extends \DAO {
protected function initDaoProperties() {
$this->table = "storedfiles";
}
}
CREATE TABLE IF NOT EXISTS `storedfiles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`location` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Sustituye
$fileInfos['basename']Por
basename(str_replace('\\','/',$fileInfos['basename']))
Sustituye
basename($request->pdf_file)Por
basename(str_replace('\\','/',$request->pdf_file))