2

I have been trying to modify an XML document within an archived .tgz file by using the PharData class using php. I have no problem modifying the XML through DomDocument class, but I am having a hard time saving the whole process: I end up saving 2 different .phar documents alltogether, w/o modifying anything in the .tgz archive :

folder example image

Here is the code I have been using :

            $phar = new PharData($path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file);

            echo $phar . "\n";

            $dom = new DOMDocument();
            $dom->load ('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '/' . substr($file, 0, -4) . '.xml');

            if ($dom === null) continue;
            $ids = $dom->documentElement->getElementsByTagName('id');

            foreach ($ids as $i_id) {
                if ($i_id->nodeValue !== $id) continue;
                echo $i_id->nodeValue . "\n";
                echo $i_id->nodeName . "\n";
                $sensor = $i_id->parentNode;
                echo $sensor->nodeName . "\n";
                foreach ($sensor->childNodes as $object) {
                    if ($object->nodeName !== 'type') continue;
                    echo $object->nodeName . "\n";
                    echo $object->nodeValue . "\n";
                    $object->nodeValue = $type;
                    echo $object->nodeValue . "\n" . "\n";
                }       
            }
            $dom->save('phar://' . $path . '/' . $idClient . '/' . $tempDate->format('ym') . '/' . $file . '.phar' . '/' . substr($file, 0, -4) . '.xml');
SkarEye
  • 21
  • 1
  • 1
    What do you get if just untar the file without decompressing? It should be a gz compressed file. – LMC Jul 21 '21 at 13:32
  • If I were to untar I would have to use extractTo, right? That would then mean extracting the files from the tar I imagine – SkarEye Jul 21 '21 at 13:47
  • Inspect it on command line first I would say so you know what you are dealing with. Then try to work with the tgz in PHP. I don't think it's possible to edit the file inside the tgz so, it's unpacking-packing issue in my opinion. Worth taking a look [here](https://stackoverflow.com/a/1191069/2834978) probably. – LMC Jul 21 '21 at 13:55

0 Answers0