File Upload/Download
How to upload and download files?
To upload and download files we will use the following screen.
To implement this application, we need to create
1) Template file (.tpl file)
2) Class file
3) Database table (.fmt file)
First let's create the template file with the following code.
file.tpl
<h5>Upload</h5>
<form id="form_fileupload">
<input type="file" name="uploadfile">
</form>
<button class="ajax-link" data-form="form_fileupload" data-class="{$class}" data-function="upload_file_exe">
upload</button>
<div style="clear:both;border-bottom: 1px #ccc solid;width:100%;padding-top: 20px;"></div>
<div class="filelist">
<h5>Download</h5>
<ul>
{foreach $filelist as $data}
<li style="display: block;float: left;clear: left;">
<button class="download-link" data-class="{$class}" data-function="download_file" data-filename="{$data.filename}" data-id="{$data.id}">Download {$data.filename}</button>
</li>
{/foreach}
</ul>
</div>
In the above code download button class should be "download-link". data-filename and data-id properties are set on the download button.
Class file sample code is shown below.
uploadfile.php
class uploadfile {
private $ffm_file;
function __construct(Controller $ctl){
//create database table connection
$this->ffm_file = $ctl->db("file");
}
function upload_file_exe(Controller $ctl){
//get file name
$filename = $ctl->get_posted_filename("uploadfile");
//check whether filename is empty
if(!empty($filename)){
$data = array();
$data["filename"] = $filename;
//insert data to the file table
$this->ffm_file->insert($data);
//make file name unique by adding "id"
$save_filename = "file-". $data["id"];
//save uploaded file
$ctl->save_posted_file("uploadfile", $save_filename);
}
//reload view
$this->file_page($ctl);
}
function download_file(Controller $ctl){
//Retrieve file id
$id = $ctl->POST("id");
//Make file name appending $id
$save_filename = "file-". $id;
//Display uploaded file
$ctl->res_saved_file($save_filename);
}
function file_page(Controller $ctl){
//Retrieve all file names stored in db
$filelist = $this->ffm_file->getall();
//Assign retrieved file name list to "filelist" parameter
$ctl->assign("filelist", $filelist);
//Display multi dialog window
$ctl->show_multi_dialog("file_up_down", "file.tpl", "file upload/download", 800, true,true);
}
Database table (.fmt file) content is shown below.
file.fmt
id,24,N
filename,255,T