Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

Procedure to open file objects

Locked
massiws
Posts: 503
Joined: Thu May 24, 2012 2:08 am
Location: Milan, Italy
Contact:

Procedure to open file objects

Unread post by massiws »

Hi, following wiki suggestions (http://wiki.nubuilder.com/tiki-index.ph ... uilderDocs) I set up a procedure to open file objects stored in db (untill now all PDF files).
The procedure works fine, but I have hardcoded table and field names of each object (as in the wiki page).

Now I have to add some new forms, many of them with many file objects inside (each form contains 3/4 file objects) where users want to upload pdf, doc, txt, xls files.
So I want to improve the code to open each file object, using 3 hash variables.

In Double Click field of each object I insert this JavaScript code:

Code: Select all

var pTable  = 'my_table_name';                   // name of the table to search in
var pField  = 'my_field_name';                   // field containing the BLOB data
var docName = $('#'+pField+'_file_name').val();  // name of the file to open
if (docName) {
    runIt('openDoc', pTable, pField, docName);
}
In Add Activity I insert this openDoc file:

Code: Select all

<?php

/*
 * 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

// Array of allowed document types
// Uncomment all extensions you want to allow download
$exts = array(
    // 'js',
    // 'php',
    // 'png',
    // 'jpg',
    // 'jpeg',
    'xls',
    'doc',
    'txt',
    'pdf'
);

// Get file extension
$ext = substr(strrchr($docName,'.'),1);

// Check if the file extension is allowed
if (!in_array($ext, $exts)) {
    nuDebug(nuTranslate("File extension not allowed: ".$ext));

} else {
    // 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->$docName;                   // ***
        $type = $row->{$fldName . "_file_type"};  // ***
    
        header("Content-type: ".$type);
        header("Content-Disposition: attachment; filename=".$docName);
        header("Pragma: private");
        header("Cache-control: private, must-revalidate");

        // show the document 
        echo $file;
    }
  
}
The browser download file correctly, but it can't be opened (corrupted?).
Befor heade() instructions, with nuDebug I see $type and $docName variables have the right values, but nothing in $file variable.
If I hardcode the object attributes in two lines marked '***' files can be opened:

Code: Select all

   $file = $row->my_document_name;  // ***
   $type = $row->my_field_type;     // ***
How can I solve?
Thanks in advance.
Max
massiws
Posts: 503
Joined: Thu May 24, 2012 2:08 am
Location: Milan, Italy
Contact:

Re: Procedure to open file objects

Unread post by massiws »

Solved!
At the end it was a trivial syntax error!!

I want to share the code, hoping to be useful to someone.
This is the activity 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;
    }
  
} else {
   nuDebug(nuTranslate('Download not allowed'));
}
I also added a new translation string in zzsys_translate

Code: Select all

INSERT INTO `dbbfd`.`zzsys_translate` (`zzsys_translate_id`, `trl_language`, `trl_english`, `trl_translation`) VALUES ('', 'it', 'Download not allowed', 'Download non permesso');
To call this procedure you have to insert this in On Double Click field of the file object you want to open:

Code: Select all

var pTable  = 'your_table-name';
var pField  = 'your_blob_field';
var docName = $('#'+pField+'_file_name').val();
if (docName) {
    runIt('openDoc', pTable, pField, docName);
} else {
    alert('Impossibile aprire il file');
}
Max
admin
Site Admin
Posts: 2781
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5
Been thanked: 1 time

Re: Procedure to open file objects

Unread post by admin »

Thanks!
Locked