Click on button and save xml file on server side

0
Hello, I am building an application with PHP, it happens that I have a button that when clicking it generates a file.xml in the downloads folder, I believe that by default, but I want to generate this file.xml in a specific folder that stores my files .xml.

I saw some examples using the preg_match function, but it only changed the file name by adding an underline or dot, but it did not save in the destination folder.

This is my code to generate the person xml:

<?php
  class RetornaPessoa_XML{
        public function Pessoa_xmlGen($pessoaweb_id){
             require_once('connection.php');
             $sql = "SELECT pessoas.pessoaweb_id,
                            pessoas.nome,
                            pessoas.cpf_cnpj,
                            pessoas.rg_ie
             FROM   pessoas
             WHERE  pessoas.pessoaweb_id =".$pessoaweb_id;

       $stmt = ibase_query($conn, $sql);
       $data = [];
       $i = 0;
       $fileName = "../_xxx_Pessoa_id-".$pessoaweb_id;
      while($row = ibase_fetch_assoc($stmt)){
       $data[$i]['pessoaweb_id'] = $pessoaweb_id;
       $data[$i]['nome'] = $row['NOME'];
       $data[$i]['cpf_cnpj'] = $row['CPF_CNPJ'];
       $data[$i]['rg_ie'] = $row['RG_IE'];
       $i++;
      }
//CHAMA A CLASSE EXPORT, COLOCA DENTRO DE UMA VARIÁVEL
    //E EM SEGUIDA USA SUA FUNÇÃO, QUE MONTA E RETORNA UM ARQUIVO XML
    require_once('pessoa_setXML.php');
    $export = new Export();

    if(isset($pessoaweb_id)){
        $export->xml($fileName, $data, $pessoaweb_id);
    }
    // echo "<script>alert('chegou 2!');</script>";
    return true;
    }
 }  
?>

AND HERE IS THE CODE TO SET THE XML, MOUNT THE XML WITH THE PARAMETERS PROVIDED     

class Export{

public function xml($fileName, $data, $pessoaweb_id){
//RECEBE AS INFORMAÇÕES POR PARÂMETRO E MONTA O XML COM OS DADOS FORNECIDOS

    $file = "{$fileName}.xml";
    $xml = "<?xml version='1.0' encoding='UTF-8' ?>";
    $xml .= "<DATAPACKET VERSION='1.0'>";
    $xml .= "<METADATA>";
    $xml .= "<FIELDS>";
    $xml .= "<FIELD WIDHT='64' FIELDTYPE='Integer' ATTRNAME='PESSOA_ID'/>";
    $xml .= "<FIELD WIDHT='64' FIELDTYPE='Integer' ATTRNAME='NOME'/>";
    $xml .= "<FIELD WIDHT='64' FIELDTYPE='Integer' 
    ATTRNAME='NOMEFANTASIA'/>";
    $xml .= "<FIELD WIDHT='300' FIELDTYPE='Integer' ATTRNAME='CPF_CNPJ'/>";
    $xml .= "<FIELD WIDHT='300' FIELDTYPE='Integer' ATTRNAME='RG_IE'/>";
    $xml .= "<FIELD WIDHT='300' FIELDTYPE='Integer' 
    ATTRNAME='PESSOAWEB_ID'/>";
    $xml .= "</FIELDS>";        
    $xml .= "</METADATA>";      
    $xml .= "<{$fileName}>";
    $xml .= "<ROWDATA>";        
    for($i = 0; $i < count($data);$i++){
        $xml .= "<ROW>";
    $xml .= "<ROW PEDIDOWEB_ID='{$data[$i]['pessoaweb_id']}' 
RG_IE='{$data[$i]['rg_ie']}'
                  CPF_CNPJ='{$data[$i]['cpf_cnpj']}' 
NOMEFANTASIA='NOMEFANTASIA' 
                  NOME='{$data[$i]['nome']}' PESSOA_ID='PESSOA_ID'/>";
        $xml .= "</ROW>";
    }

    $xml .= "</ROWDATA>";       
    $xml .= "</{$fileName}>";       
    $xml .= "</DATAPACKET>";

    // CONFIGURA O HEADER PARA DOWNLOAD
    header("Content-Description: PHP Generated Data");
    header("Content-Type: application/xml");
    header("Content-Disposition: attachment; filename=\"{$file}\"");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");

    // ENVIA CONTEÚDO
    echo $xml;
}
}
    
asked by anonymous 22.05.2018 / 15:35

0 answers