Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Button that Generates a File

Questions related to using nuBuilder Forte.
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Button that Generates a File

Unread post by Janusz »

Hi,
Try first if your your folder has proper rights for writing.
the following example works OK on my application (with DEBIAN):
https://www.w3schools.com/php/php_file_create.asp
For a quick test I created php procedurę called my_file with following code inside:

Code: Select all

$myfile = fopen("./uploads/newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
and has tested it with buton:
onclick nuRunPHPHidden('my_file');
If you like nuBuilder, please leave a review on SourceForge
Alohajoe5
Posts: 55
Joined: Tue Apr 16, 2019 1:32 pm

Re: Button that Generates a File

Unread post by Alohajoe5 »

Ok, so inserting the following code as a hidden php procedure:

Code: Select all

$con = nuRunQyery($sql);
$sql = "SELECT * FROM My_View";
$result = $con->query($sql);
$rowcount = 0;
$filecount = 1;

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()){

// build logic for splitting file.
$rowcount = $rowcount + 1;

if (($rowcount % 10000) == 0) {
  $filecount = $filecount + 1;
}
// join row elements into a string
$rowstr = join(",",$row);

//String Replace commas with Pipes
$rowstr2 = str_replace (',', '|', $rowstr);

//Count number of pipes
$fields = substr_count($rowstr2, '|');

//Calculate the number of empty fields & insert pipes
$emptyfields = str_repeat('|', (26-$fields));

//Concatenate data with empty fields
$rowstr2 .= $emptyfields;
$rowstr2 .= PHP_EOL;
$filename = 'signals2_' . $filecount . '.dat';

//Write data to file
file_put_contents ($filename, $rowstr2, FILE_APPEND);
    }//end while
}
and having the Button object set up as: onclick nuRunPHPHidden('Signals_Outfiles);

Results in this error when pressing the button:
"Procedure Signals_Outfiles
/var/www/html/nucommon.php(1254) eval()'dcode
call to a member function query () on array

Traced from...

(line:48)/var/www/html.nuapi.php -nuRunPHPHidden
(line:381)/var/www/html/nucommon.php - nuEval
(line:1254)/var/www/html/nucommon.php -eval"

No outfiles are generated from the PHP.
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Button that Generates a File

Unread post by Janusz »

first I would check if the simple code like belowe is properly saving the file in the folder - did you maybe try this?
(just replace only your code with following code inside the Signals_Outfiles php procedure and check if the file is saved - and adjust the path to your case)

Code: Select all

$myfile = fopen("./uploads/newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
If you like nuBuilder, please leave a review on SourceForge
Alohajoe5
Posts: 55
Joined: Tue Apr 16, 2019 1:32 pm

Re: Button that Generates a File

Unread post by Alohajoe5 »

I have resolved this issue by using the proper calls. Is there any way of making this a non-hidden process that would allow a user to generate files and then download them directly?
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Button that Generates a File

Unread post by Janusz »

normally the files are saved on the server and later you can access them depending on the needs with FTP like filezilla
or with the html link for example:
https://your.web.page/uploads/my_file.txt
If you like nuBuilder, please leave a review on SourceForge
Post Reply