Button that Generates a File

Questions related to using nuBuilder Forte.

Re: Button that Generates a File

Postby Janusz » Tue Jul 30, 2019 12:50 am

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');
Janusz
 
Posts: 280
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Button that Generates a File

Postby Alohajoe5 » Tue Jul 30, 2019 2:18 am

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.
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby Janusz » Tue Jul 30, 2019 2:41 am

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);
Janusz
 
Posts: 280
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Button that Generates a File

Postby Alohajoe5 » Tue Jul 30, 2019 2:55 am

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?
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby Janusz » Tue Jul 30, 2019 3:37 am

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
Janusz
 
Posts: 280
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Previous

Return to General



cron