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);
}
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;
}
}
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; // ***
Thanks in advance.
Max