0

I have a XML structure in my PHP file.

For example:

$file = file_get_contents($myFile);

$response = '<?xml version="1.0"?>';
$response .= '<responses>';
$response .= '<file>';
$response .= '<name>';
$response .= '</name>';
$response .= '<data>';
$response .= base64_encode($file);
$response .= '</data>';
$response .= '</file>';
$response .= '</responses>';

echo $response;

If i create .doc file or with other extension and put little text in, it works. But, if user load file with complex structure (not only text) - XML just not load, and i have a empty file without errors.

But the same files works on my other server.

I have try use simplexml_load_string for output errors, but i have no errors.

The server with PHP 5.3.3 have the problem; the one with PHP 5.6 hasn’t. It works if I try it with 5.3.3 on my local server.

Is the problem due to the PHP version? If so, how exactly?

bfontaine
  • 18,169
  • 13
  • 73
  • 107
vasya
  • 35
  • 2
  • 6
  • Have you checked page source? Maybe you just don't see not closed tag with all data? – Justinas Jan 19 '17 at 08:44
  • no, i check it. Same code works on other server. It is not about tags close – vasya Jan 19 '17 at 08:53
  • Have you turned all errors on? – Justinas Jan 19 '17 at 09:00
  • Ooh, i think i find where the problem is... Allowed memory size fatal error), i did not have logs access for this moment. Ty for you reply, Justinas. – vasya Jan 19 '17 at 09:09
  • 1
    A blank page (or a "500 Internal Server Error") means your script is throwing an error but PHP is configured to hide it from you. You need to fix it ASAP because coding without the aid of error messages is hard. As quick start, you can set the `error_reporting` and `display_errors` directives in your computer's system-wide `php.ini` file ([details here](http://stackoverflow.com/a/5680885/13508)). Errors thumb rule: show in development, log in production. – Álvaro González Jan 19 '17 at 09:28
  • Alvaro, hi, ty for reply. Yes, i know it and i turn on all errors, and i see them all, except "Allow memory size fatal error". It confused me. But now, i solved the problem: change memory_limit size – vasya Jan 19 '17 at 09:41

1 Answers1

1

There're basically three things that can be improved in your code:

  1. Configure error reporting to actually see error messages.

  2. Generate XML with a proper library, to ensure you cannot send malformed data.

  3. Be conservative in memory usage (you're currently storing the complete file in RAM three times, two of them in a plain text representation that depending of file type can be significantly larger).

Your overall code could like like this:

// Quick code, needs more error checking and refining
$fp = fopen($myFile, 'rb');
if ($fp) {
    $writer = new XMLWriter();
    $writer->openURI('php://output');
    $writer->startDocument('1.0');

    $writer->startElement('responses');
        $writer->startElement('file');
            $writer->startElement('name');
            $writer->endElement();
            $writer->startElement('data');
            while (!feof($fp)) {
                // If I recall correctly, substring size must be multiple of 4 
                // to encode it properly (except for last part)
                $writer->text(base64_encode(fread($fp, 10240)));
            }
            $writer->endElement();
        $writer->endElement();
    $writer->endElement();

    fclose($fp);
}

I've tried this code with a 316 MB file and used 256 KB on my PC.

As a side note, inserting binary files inside XML is pretty troublesome when files are large. It makes extraction problematic because you can't use most of the usual tools due to extensive memory usage.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360