Welcome to the nuBuilder Forums!

Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.

File Upload and Download

Locked
Tinka
Posts: 73
Joined: Mon Feb 24, 2014 2:58 pm

File Upload and Download

Unread post by Tinka »

I want to be able to upload and download pdf files stored as BLOB in the db (or an upload directory on the webserver), not an external file service.
One file should be linked to one record.

The elegant solution here is for Nubuilder 2, but Nubuilder 3 has deserted the File object:

http://forums.nubuilder.cloud/viewtopic.p ... ile#p11460

The upload part works fine when using this on After Save of the upload form:

Code: Select all

$J           = json_decode("#FILES#");

if($J[0]->name == ''){return;}

$dt          = new DateTime();
$date        = $dt->format('Y-m-d H:i:s');

$name        = $J[0]->name;
$type        = $J[0]->type;
$size        = $J[0]->size;

$uploaddir   = sys_get_temp_dir();
$contents    = file_get_contents($uploaddir . '/' . $J[0]->name);
$contents    = addslashes($contents);

$s           = "
UPDATE zzzsys_file 
SET 
sfi_blob                   = '$contents',  
sfi_name                   = '$name',  
sfi_type                   = '$type',  
sfi_size                   = '$size', 
zzzsys_file_log_changed_at = '$date'

WHERE zzzsys_file_id       = '#RECORD_ID#'
";

nuRunQuery($s);

unlink('tmp/' . $J[0]->name);
I have looked in the php code FILE, nugetfile.php and here:
http://wiki.nubuilder.net/index.php/Mis ... pload_Form
http://wiki.nubuilder.com/tiki-index.ph ... uilderDocs

but I can not figure out how to solve the download part and linking to the record.

I know I need to set the header like in this code:

Code: Select all

/*
* Get record from db using parameter passed through runIt() via hash variables
*/

// Get parameters from hash variables
$tblName = '#runIt1#';    // table name to search in
$fldName = '#runIt2#';    // field name containing BLOB data
$docName = '#runIt3#';    // file to open

// Allowed file extension
// Uncomment all extensions you want to allow download
$allowedExtensions = array(
    'txt',
//  'html',
//  'htm',
//  'exe',
    'zip',
    'doc',
    'xls',
//  'ppt',
//  'gif',
//  'png',
//  'jpeg',
//  'jpg',
//  'php',
    'pdf'
); 

// Check if allowed download (only globeadmin can download all file)
$ext = strtolower(substr(strrchr($docName,"."),1));
if (array_key_exists($ext, $allowedExtensions) || ('#zzsys_user_id#' == 'globeadmin')) {

    // Build and execute the query
    $sql = "SELECT * FROM $tblName WHERE " . $fldName . "_file_name = '$docName' ";
    $row = db_fetch_object(nuRunQuery($sql));

    if ($row) {
        // Get data from DB
        $file = $row->$fldName;
         $type = $row->{$fldName . "_file_type"};

        header("Content-type: ".$type);
        header('Content-Disposition: attachment; filename="'.$docName.'"');
        header("Pragma: private");
        header("Cache-control: private, must-revalidate");
        header("Content-Transfer-Encoding: binary"); 

        // show the document 
        print $file;
    }
Any help is appreciated.

Tinka
admin
Site Admin
Posts: 2825
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: File Upload and Download

Unread post by admin »

Tinka,

See if this helps (you'll need the latest version)..

http://wiki.nubuilder.net/index.php/Mis ... base_table

Steven
Tinka
Posts: 73
Joined: Mon Feb 24, 2014 2:58 pm

Re: File Upload and Download

Unread post by Tinka »

Hi Steven

The Upload works fine.

Thank you for the download function nuDownloadFile -but I can't make it work.

I followed instructions here:
http://wiki.nubuilder.net/index.php/Mis ... base_table,
and get the right data from another table selected, as I can see in the Debug.
A new tab opens when the php code is run, but nothing happens (no file is downloaded).

I was wondering about the $i, because in the url in the php window it is different every time and does not match the #RECORD_ID#.

Is this about security or PHP version? I updated from within Nubuilder, the first time it makes an error that file size does not match, second time it went fine. Anyway, I copied the function in nucommon.js since it was not there after the update?!


Here is my PHP code which I run off a the button event OnClick nuRunPHP('download');

Code: Select all

$s = " 
SELECT 

fil_type AS file_type, 
fil_name AS file_name, 
fil_blob AS file_blob 
FROM files
WHERE fil_id = '#RECORD_ID#'
 
";
nuDebug($sql);

nuDownloadFile('', $s);
BR, Tinka
Tinka
Posts: 73
Joined: Mon Feb 24, 2014 2:58 pm

Re: File Upload and Download

Unread post by Tinka »

Now the download of any file type works! No code changed.

But instead of updating from inside Nubuilder, I updated all files directly from GitHub, and changed the config.php (and other changed files).

After logout, the login page was blank, so I knew something had went wrong with the Update from inside Nubuilder, I also received the same error as in this post:

http://forums.nubuilder.cloud/viewtopic.php?f=17&t=8626

Also, nuinstall.php could not be run even after update of config.php.

You can close this topic, but please look at the Update from the Administration screen.

BR, Katja
admin
Site Admin
Posts: 2825
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: File Upload and Download

Unread post by admin »

.
Locked