iterate a read_data to store filenames, put in a zip and download

gix.vax 15 Feb, 2020
hi all

i'm trying to putting a list of files in a zip to download
event: download_zip
{fn:read_selected} (my file list)
{fn:crea_zip} (a php custom code)

read_selected works ok and i have to take my filenames from documento (joining with a path in php)
filename element is "documento"[pre]Array
(
[read_selected] => Array
(
[log] => Array
(
[0] => SELECT `selected`.`aid` AS `selected.aid`, `selected`.`user_id` AS `selected.user_id`, `selected`.`created` AS `selected.created`, `selected`.`modified` AS `selected.modified`, `selected`.`cliente` AS `selected.cliente`, `selected`.`azienda` AS `selected.azienda`, `selected`.`uuidcli` AS `selected.uuidcli`, `selected`.`uuidazi` AS `selected.uuidazi`, `selected`.`uuiddip` AS `selected.uuiddip`, `selected`.`uuiddoc` AS `selected.uuiddoc`, `selected`.`idtipodoc` AS `selected.idtipodoc`, `selected`.`documento` AS `selected.documento`, `selected`.`descrizione` AS `selected.descrizione`, `selected`.`tipodoc` AS `selected.tipodoc`, `selected`.`datadoc` AS `selected.datadoc`, `selected`.`dadata` AS `selected.dadata`, `selected`.`adata` AS `selected.adata`, `selected`.`scaricato` AS `selected.scaricato`, `selected`.`data_scaricato` AS `selected.data_scaricato`, `selected`.`tstamp` AS `selected.tstamp` FROM `vuxdoccli` AS `selected` WHERE `selected`.`uuiddoc` IN ('test1', 'test2', 'test2', 'test2') GROUP BY `selected.uuiddoc`;[br] )[br][br] [var] => Array[br] ([br] [0] => Array[br] ([br] [selected] => Array[br] ([br] [aid] => 1[br] [user_id] => 0[br] [created] => 0000-00-00 00:00:00[br] [modified] => 2020-02-09 15:44:02[br] [cliente] => test1[br] [azienda] => test1AZI[br] [uuidcli] => cliente1[br] [uuidazi] => uuidtest1azi[br] [uuiddip] => [br] [uuiddoc] => test1[br] [idtipodoc] => 45[br] [documento] => test1.pdf[br] [descrizione] => Giudizio[br] [tipodoc] => CERT_EL_CLI[br] [datadoc] => 2020-01-29 00:00:00[br] [dadata] => 2020-01-01 00:00:00[br] [adata] => 2020-01-31 00:00:00[br] [scaricato] => 1[br] [data_scaricato] => 2020-02-09 15:44:02[br] [tstamp] => 2020-02-14 23:50:59[br] )[br][br] )[br][br] [1] => Array[br] ([br] [selected] => Array[br] ([br] [aid] => 2[br] [user_id] => 0[br] [created] => 0000-00-00 00:00:00[br] [modified] => 2020-02-09 15:44:11[br] [cliente] => test1[br] [azienda] => test2AZI[br] [uuidcli] => cliente1[br] [uuidazi] => [br] [uuiddip] => [br] [uuiddoc] => test2[br] [idtipodoc] => 46[br] [documento] => test2.pdf[br] [descrizione] => Registro[br] [tipodoc] => REGiSTRO_VISITE[br] [datadoc] => 2019-12-01 00:00:00[br] [dadata] => 2019-01-01 00:00:00[br] [adata] => 2019-12-31 00:00:00[br] [scaricato] => 1[br] [data_scaricato] => 2020-02-09 15:44:11[br] [tstamp] => 2020-02-14 23:51:17[br] )[br][br] )[br][br] )[br][br] )[br][br])[/pre]

now, with php, i want to take array, iterate with foreach and pass filenames ("documento") to php zip function

this is my php ({fn:crea_zip}) VAR ONLY RESULTS
 // todo :
// verify if files exists
// give zip file a unique name and destroy after download
// debug

// docs folder
$pathbase="/mydocs/";

// zip file to create
$pathzip=$pathbase."/zip/";
$nomezip=$pathzip."test.zip";

//THIS part, i think, doesn't wok, i can't take array
// i try a lot of combinations but i can't understand the right syntax
$array_file=array();
$this->get("read_selected","default");
$array_file=$this->data("read_selected");

$zip = new ZipArchive();

// iterate array
foreach ($array_file as $filedatrattare) {

$fileattuale=$filedatrattare["documento"];
// add files to zip
$aggiungizip=$pathbase.$fileattuale;
if($zip->open($nomezip, ZIPARCHIVE::CREATE)) {
// se esiste il file lo aggiunge
if (file_exists($aggiungizip)) {
$zip->addFile($aggiungizip);
echo "NOME FILE ".$aggiungizip;
}else{
echo 'il file NON esiste : '.$aggiungizip;
}
}
$zip->close();
unset($zip);
}
?>
There should be a lot of logic errors after entering foreach, i'll have to debug it but it never enter foreach loop.

i think i don't take array and i can't understand if i can use {var:read_selected} in some way inside php[br]if i activate errors, it says [br]warning: invalid argument supplied for foreach....[br][br]can you correct my code so i can understand how to iterate read_selected (every element is a record, so after i have to point them to take filenames)[br]
i need it in php
[br]thank you

edit: typo
gix.vax 16 Feb, 2020
i simplify a bit all the things but IT DOESN'T WORK

my read_selected now is a key->value pair
Array
(
[read_selected] => Array
(
[log] => Array
(
[0] => SELECT `selected`.`uuiddoc` AS `selected.uuiddoc`, `selected`.`documento` AS `selected.documento` FROM `vuxdoccli` AS `selected` WHERE `selected`.`uuiddoc` IN ('test1', 'test2', 'test2', 'test2') GROUP BY `selected.uuiddoc`;
)

[var] => Array
(
[test1] => test1.pdf
[test2] => test2.pdf
)

)

)
my PHP code

$pathbase="/doczcli/";
$pathzip="/doczcli/zip/";
$nomezip=$pathzip."test.zip";

//??????????????????????????????????????????
$arr_file=array();
$arr_file=$this->get("read_selected","default");

$zip = new ZipArchive();
// probably it doesn't take array and foreach give a parameter error
foreach ($arr_file as $key=>$value) {

$fileattuale=$value;
$aggiungizip=$pathbase.$fileattuale;
if($zip->open($nomezip, ZIPARCHIVE::CREATE)) {

if (file_exists($aggiungizip)) {
$zip->addFile($aggiungizip);

}else{
echo 'can't add this file to zip : '.$aggiungizip;
}
}
$zip->close();
unset($zip);
}
?>
healyhatman 16 Feb, 2020
Need to be a lot clearer than "doesn't work". What's not working, are there errors, what?
gix.vax 16 Feb, 2020
hi healyhatman

i wrote it
if i activate errors, it says " warning: invalid argument supplied for foreach...."

if i print_r($arr_file)
nothing shown

{debug:} show right data

my syntax is correct?
i need it in php cause i have to implement other functions and need to know the right way to iterate array.

i try too
 $arr_file=$this->get("read_selected[]", "default");
 $arr_file=$this->get("read_selected()", "default");
 $arr_file=$this->get("read_selected.selected", "default");
i can't understand where is the error and i'm blocked and need it early, are 2 days i'm on this function.
gix.vax 16 Feb, 2020
i tested my code to zip and it works, i need only my array

this is last version (commented section is working can create zip and can copy a file)

echo "START HERE
";

$rootfolder=$_SERVER['DOCUMENT_ROOT'];

$pathbase=$rootfolder."/doczcli/";
$pathzip=$rootfolder."/doczcli/zip/";
$nomezip=$pathzip."test.zip";

//var:read_selected in array
$arr_file=array();
$arr_file=$this->get("read_selected.selected", "default");

echo "BEFORE foreach....
path-nomezip:".$nomezip."
";

$zip = new ZipArchive();

// test : this part work
// $aggiungizip=$pathbase."test1.pdf";
// $aggiungizip2=$pathbase."test1xxxxxxxxxx.pdf";
// $nomezip1=$pathbase."/provadizipfile.zip";
// test di copia file ok
// copy($aggiungizip,$aggiungizip2);
// $zip->open($nomezip1, ZIPARCHIVE::CREATE);
// $zip->addFile($aggiungizip);
// $zip->close();
// unset($zip);
//test -test- testtest -test- test test -test- testtest -test- testtest -test- testtest -test- test

// ITERATE array, take doc name and put in zip if exist
foreach ($arr_file as $key=>$value) {
$fileattuale=$value;
$aggiungizip=$pathbase.$fileattuale;
echo "working file: ".$fileattuale." --- complete path:".$aggiungizip."
";
if($zip->open($nomezip, ZIPARCHIVE::CREATE)) {
// if file exist add to zip
if (file_exists($aggiungizip)) {
$zip->addFile($aggiungizip);
echo "NOME FILE ".$aggiungizip;
}else{
echo "file doesn't exists :".$aggiungizip;
}
}
$zip->close();
unset($zip);
}
echo "THIS IS THE END";
?>



where is my array?

🤗
gix.vax 16 Feb, 2020
if i use[pre]$arr_file=array();
$arr_file=$this->get("read_selected","default");[/pre]
i have an error page with
"Call to a member function open() on null"
(in joomla i acitvate development level for errors)

if i use[pre]$arr_file=array(); $arr_file=$this->get("read_selected()","default");
or[br]..read_selected.something[/pre]
no error page
but error in execution:
Warning: Invalid argument supplied for foreach() in /var/www/vhosts/mysito/httpdocs/libraries/cegcore2/admin/extensions/chronofc/helpers/parser.php(133)[br] : eval()'d code on line 41

putting CC6 syntax to debug array (closing and reopening php tag)
{var:read_selected} [br]it show my array in this way
{"test1":"test1.pdf","test2":"test2.pdf"}

var_dump($arr_file);
show : NULL
healyhatman 16 Feb, 2020
Just to make sure, is the read data action happening BEFORE your PHP code?

And don't literally type "default" as the second argument, that's an optional default parameter if the thing you're trying to get doesn't exist.
gix.vax 16 Feb, 2020
Answer
read_data is ok

now work
the problem i think was i set and unset zip file inside foreach and it give an error that put me on the wrong way.

when i try to select only 1 file, it create zip (you know how a man could be happy in this situations 🤗)
then i debug inside foreach


for future use (i think is an useful function to download selected files) i post my code
take and enjoy
// if zip file exist, it overwrite, change file name with a unique name if you need to manage more files
// variable zipok say if at least 1 file is in zip file
// folders
// base path of server (in filesystem /var/www/... , not in www.mysite.com/....)
$rootfolder=$_SERVER['DOCUMENT_ROOT'];
// build paths for file to zip and zip file
$pathbase=$rootfolder."/doczcli/";
$pathzip=$rootfolder."/doczcli/zip/";
$nomezip=$pathzip."test.zip";

//my array is in var:read_selected and is a key=>value
$arr_file=array();
$arr_file=$this->get("read_selected","default");

//create a zip
$zip = new ZipArchive();
$zip->open($nomezip, ZIPARCHIVE::CREATE);

// ---- ITERATE array, take doc name and put in zip if exist----
$zipok=0; //if remain 0 after foreach, you know there's no files in zip file
foreach ($arr_file as $key=>$value) {
$fileattuale=$value;
$aggiungizip=$pathbase.$fileattuale;

// if file exist add to zip
if (file_exists($aggiungizip)) {
$zip->addFile($aggiungizip);
//hey, there's a file, i'm happy to say to all in my zipok var
$zipok=1;
}
}
$zip->close();
unset($zip);
// here you can store variable with filename or open/save file or whatever you want
// if you use a unique name for file, remember to clean folder or delete zip file after download or your folder grow
?>
This topic is locked and no more replies can be posted.