Buy Now
Sign in

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

gix.vax , February 15 2020, 13:37
G
gix.vax 119
February 15 2020, 13:37 #391192
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"
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`;
)

[var] => Array
(
[0] => Array
(
[selected] => Array
(
[aid] => 1
[user_id] => 0
[created] => 0000-00-00 00:00:00
[modified] => 2020-02-09 15:44:02
[cliente] => test1
[azienda] => test1AZI
[uuidcli] => cliente1
[uuidazi] => uuidtest1azi
[uuiddip] =>
[uuiddoc] => test1
[idtipodoc] => 45
[documento] => test1.pdf
[descrizione] => Giudizio
[tipodoc] => CERT_EL_CLI
[datadoc] => 2020-01-29 00:00:00
[dadata] => 2020-01-01 00:00:00
[adata] => 2020-01-31 00:00:00
[scaricato] => 1
[data_scaricato] => 2020-02-09 15:44:02
[tstamp] => 2020-02-14 23:50:59
)

)

[1] => Array
(
[selected] => Array
(
[aid] => 2
[user_id] => 0
[created] => 0000-00-00 00:00:00
[modified] => 2020-02-09 15:44:11
[cliente] => test1
[azienda] => test2AZI
[uuidcli] => cliente1
[uuidazi] =>
[uuiddip] =>
[uuiddoc] => test2
[idtipodoc] => 46
[documento] => test2.pdf
[descrizione] => Registro
[tipodoc] => REGiSTRO_VISITE
[datadoc] => 2019-12-01 00:00:00
[dadata] => 2019-01-01 00:00:00
[adata] => 2019-12-31 00:00:00
[scaricato] => 1
[data_scaricato] => 2020-02-09 15:44:11
[tstamp] => 2020-02-14 23:51:17
)

)

)

)

)
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
if i activate errors, it says
warning: invalid argument supplied for foreach....

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)
i need it in php

thank you
edit: typo
G
gix.vax 119
February 16 2020, 00:02 #391193
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 9
February 16 2020, 05:53 #391197
Need to be a lot clearer than "doesn't work". What's not working, are there errors, what?
Web developer at SkySpider.com.au - I DO NOT work for ChronoEngine!
Donations: paypal.me/healyhatman
Professional Work: kamron@skyspider.com.au
Custom CF / CC Plugins and Extensions: skyspider.com.au/store
G
gix.vax 119
February 16 2020, 11:23 #391199
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.
G
gix.vax 119
February 16 2020, 14:46 #391201
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?
G
gix.vax 119
February 16 2020, 18:57 #391205
if i use
$arr_file=array();
$arr_file=$this->get("read_selected","default");
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
$arr_file=array(); $arr_file=$this->get("read_selected()","default");
or
..read_selected.something
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)
: eval()'d code on line 41
putting CC6 syntax to debug array (closing and reopening php tag)
{var:read_selected}
it show my array in this way
{"test1":"test1.pdf","test2":"test2.pdf"}
var_dump($arr_file);
show : NULL

healyhatman 9
February 16 2020, 22:15 #391207
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.
Web developer at SkySpider.com.au - I DO NOT work for ChronoEngine!
Donations: paypal.me/healyhatman
Professional Work: kamron@skyspider.com.au
Custom CF / CC Plugins and Extensions: skyspider.com.au/store
G
gix.vax 119
February 16 2020, 22:25 #391210
 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
?>