PHP to generate XML file
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I'm looking for a PHP file that I can access via my browser that will allow a URL to be entered as an input and when submitted, it will generate an XML file in the same folder with the following (if the file already exists, it will overwrite the existing file):

<Response>
<Play>http://thisistheurl.com/test.mp3</Play>
</Response>
Tags
html
PHP
xml

Crowdsource coding tasks.

1 Solution

Winning solution

Hi,
this will work without using the XML library, just make sure to indent correctly the file: the XML; at line 13 must not have spaces. And to have write permissions.

<?php

$file = 'file.xml';

if(isset($_POST))
{

  $url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL);
  $template = <<<XML
<Response>
  <Play>%s</Play>
</Response>
XML;

  if($url)
  {

    $f = fopen($file, 'w+');
    fwrite($f, sprintf($template, $url));
    fclose($f);

  }

}

?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Generate XML file</title>
</head>
<body>

  <form method="post">
    <p>
      <label for="url">URL</label>
      <input type="url" name="url" id="url">
    </p>
    <p>
      <input type="submit" name="submit" value="Generate XML">
    </p>
  </form>

  <?php 

  if(file_exists($file)) {
    print sprintf('<p><a href="%s">Download XML</a></p>', $file);
  }

  ?>

</body>
</html>

Link: https://gist.github.com/cereal-s/e8a65d853a27e9fc0c858de3fba64dfc

NOTE

It is better to write to a directory which does not have permissions to execute code, especially PHP, and then add some code to download the file from the location.

Thank you!
Jay 5 months ago
Thanks to you! :)
cueball 5 months ago