简体   繁体   中英

saving xml data in php

I am trying to create and save an xml file using a database query in PHP. Code

<?php
$INC_DIR = $_SERVER["DOCUMENT_ROOT"]. "/dispatch-dev/";
include_once($INC_DIR. 'config/database.class.php');
require_once ($INC_DIR.'functions/ftp.php');

function export_xml($call_number)
{
 $db = new Database();


$sql = "SELECT * FROM nfirs_export WHERE incident_number = :call_number";
$db->bind(':call_number', $call_number);
$db->query($sql);
$row=$db->single();
$xml = new XMLWriter();
$xml->openMemory();
$file = 'parkerfd_' . $call_number . '.xml';
$xml_File = $INC_DIR. "nfirs_files/" .$file;

$xml->setIndent(true);
$xml->startDocument();

    $xml->startElement('incident');

    //call number
    $xml->startElement('incident_number');
    $xml->writeRaw($row['incident_number']);
    $xml->endElement();

    //year
    $xml->startElement('incident_year');
    $xml->writeRaw($row['incident_year']);
    $xml->endElement();


    //address number
    $xml->startElement('address_number');
    $xml->writeRaw($row['address_number']);
    $xml->endElement();

    //prefix directions
    $xml->startElement('prefix_directional');
    $xml->writeRaw($row['prefix_directional']);
    $xml->endElement();

    //street name
    $xml->startElement('street_name');
    $xml->writeRaw($row['street_name']);
    $xml->endElement();

    //street type
    $xml->startElement('street_type');
    $xml->writeRaw($row['street_type']);
    $xml->endElement();

    //cross street 1
    $xml->startElement('cross_street_1');
    $xml->writeRaw($row['cross_street_1']);
    $xml->endElement();

    //cross street 2
    $xml->startElement('cross_street_2');
    $xml->writeRaw($row['cross_street_2']);
    $xml->endElement();

    //city
    $xml->startElement('city');
    $xml->writeRaw($row['city']);
    $xml->endElement();

    //state
    $xml->startElement('state');
    $xml->writeRaw($row['state']);
    $xml->endElement();

    //incident type
    $xml->startElement('incident_type');
    $xml->writeRaw($row['incident_type']);
    $xml->endElement();

    $xml->endElement(); //incident element

    header('Content-type: text/xml');

    file_put_contents($xml_File, $xml->outputMemory());

    $xml->flush();

    send_ftp($xml_File, $file);
}

The intent of the code is to save an xml file called 'parkerfd_xxxxxxxx.xml' to the directory root/nfirs_files. Why do I not save the xml file? R

There a bunch of reasons why this isn't working.

First reason, you're accessing $INC_DIR which isn't defined in your function. You'll want to allow it to be used by adding the following code

function export_xml($call_number)
{
    global $INC_DIR;

    // rest of your code
}

Second reason, the path $INC_DIR. "nfirs_files/" $INC_DIR. "nfirs_files/" may not even exist, so it can't create the file. You'll want to make sure the folder exists, and if not create it.

if (false === is_dir($INC_DIR. "nfirs_files/")) {
    if (false === mkdir($INC_DIR. "nfirs_files/", 0755)) {
        die('failed to create folder'); // you'll need to debug this
    }
}

Third this is more of just to make your life easier. Instead of defining each key just do this.

$xml->startElement('incident');

foreach($row as $key => $value) {
    $xml->startElement($key);
    $xml->writeRaw($value);
    $xml->endElement();
}

$xml->endElement();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM