0

Even after hours of research I couldn't find any way to get php code executed through a svg image on an apache server running php.

Is it possible to execute php code in a svg image?

If yes, how?

UPDATE: The function processing the file is:

libxml_disable_entity_loader(false);
$doc = new DOMDocument();
$doc -> loadXML(file_get_contents($imageFile), LIBXML_NOENT | LIBXML_DTDLOAD); // $imageFile contains the .svg
$svg = $doc->getElementByTagName('svg');
echo $svg->item(0)->C14N();

Now I would like to execute custom PHP code that I wrote in the SVG and get the result displayed by echo $svg->item(0)->C14N();.

The PHP code must be executed on the back-end server that contains above function.

John Doe
  • 21
  • 1
  • 5
  • well the webserver needs to be told to let PHP handle the file to execute any code, otherwise its just a static asset. And since we have zero supporting information, yes it is possible. – Scuzzy Apr 24 '22 at 23:03
  • Maybe [this](https://stackoverflow.com/q/11401500/9969672) is what you are trying to do? – Janik Apr 24 '22 at 23:04

1 Answers1

0

You can configure your server to do so but that is not the best practice.

Add the forth line to httpd.conf

AddType application/x-httpd-php .php
AddType application/x-httpd-phps .phps
AddType application/x-httpd-php3 .php3 .phtml
AddType application/x-httpd-php .svg

The downside to this is every request for an .svg will take longer as it runs as a php. And that <?xml will need to be changed on every svg file since by default PHP wants to use <? as for shorthand ... not good.

Alternatively you can use .htaccess within a directory so when you call mysvg.svg it runs mysvg.php

RewriteEngine on
RewriteRule ^(.+)\.svg$ $1.php [L]

But the best practice is to send the svg from a php. Example: How do I load a SVG file that's been generated with PHP? Why hide the fact that the SVG is being dynamically created by php?


Update: If you want to run some php code triggered by an SVG being rendered.

<svg width="200" height="200"
  xmlns="http://www.w3.org/2000/svg">
  <image href="http://example.com/pixel/" height="1" width="1"/>
</svg>

Note: http://example.com/pixel/index.php script delivers a png pixel and runs some code.

Wayne
  • 4,760
  • 1
  • 24
  • 24