<?php

//phpinfo();
//$zip = new ZipArchive;
//header("Access-Control-Allow-Origin: *");
//function descargar_zip_imagenes($folio){
	$mysqli = new mysqli('localhost', 'itzitacu_caroll', 'Caroll2017', 'itzitacu_planeacion');
	//$mysqli = new mysqli('teds.mx', 'caroll_dulce', '1029384756', 'tec_zitacuaro');
	if ($mysqli->connect_errno) {
	   echo "Falló la conexión a MySQL para ".$user.": (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
	} 

	if (!$res = $mysqli->query("select * from view_dse_doc_reporte_x_documento where folio='".$_GET['folio']."'; ")) {
      echo "No se realizó select: (" . $mysqli->errno . ") " . $mysqli->error;
        exit();
    }

	$zipname = 'documentos.zip';
	$zip = new ZipArchive;
	$zip->open($zipname, ZipArchive::CREATE|ZipArchive::OVERWRITE);
	
	$i = 0;
	while ($row = $res->fetch_array(MYSQLI_ASSOC)) {
		if ($i==0) {
			$dir = utf8_encode($_GET['folio']." ".trim($row['nombre_alumno'])." ".trim($row['ap_paterno'])." ".trim($row['ap_materno']));
    		$dir = str_replace(" ", "_", $dir);
    		$zip->addEmptyDir($dir);
		}
		if ($row['fk_idPeriodo']>=5) {
			$zip->addFile($row['periodo']."/".$row['nombre'], $dir."/".$row['nombre']);
		} else {
			$zip->addFile("2018-2/".$row['nombre'], $dir."/".$row['nombre']);
		}
		
		$i++;
	}
	$zip->close();


	header('Content-Type: application/zip');
	header('Content-disposition: attachment; filename='.$zipname);
	//header('Content-Length: ' . filesize($zipname));
	readfile($zipname);
//}

?>