0

I am attempting to run the following code upon form submission to make a new directory, copy an html document the new directory, rename the html document, and save an xml with data from the html form to the directed we created. None of this is happening, but I'm not getting any errors. Any help would be greatly appreciated as I've spent hours troubleshooting this by now, and I still can't see anything wrong.

Here is the PHP script(sorry for the poor formatting, it just won't paste in cleanly):

<?php
if(isset($_POST['submit'])) {
 error_reporting(1);
 ini_set('display_errors', 'On');
 $name = $_POST["name"];
 $address = $_POST["address"];
 $photo = $_POST['photo'];
 mkdir("/kunden/homepages/9/d773272021/htdocs/articles/$name", 0755);
 chdir("/kunden/homepages/9/d773272021/htdocs/articles/$name");
 copy("/kunden/homepages/9/d773272021/htdocs/input.html", "/kunden/homepages/9/d773272021/htdocs/articles/$name");
 rename("/kunden/homepages/9/d773272021/htdocs/articles/$name/input.html", "/kunden/homepages/9/d773272021/htdocs/articles/$name/$name . '.html'");
 //XML Data In Documents
 $xmlString = "<?xml version='1.0' encoding='UTF-8'?>
    <name>$name</name>
    <address>$address</address>
    <photo>$photo</photo>
 ";
 //The above
 $dom = new DOMDocument;
 $dom->preserveWhiteSpace = TRUE;
 $dom->loadXML($xmlString);
 $dom->save("/kunden/homepages/9/d773272021/htdocs/articles/$name" . '.xml');
}
?>

Here is the code for the HTML form(it is working and running the PHP script, but I figure I might as well include it just in case):

 <!DOCTYPE html>
 <html>
  <head>
   <meta charset="UTF-8">
   <title>Article Creation Wizard (Beta)</title>
  </head>
  <body>
   <form method="post" action="output.php">
     Name: <input type="text" name="name"><br>
     Address: <input type="text" name="address" /><br>
     Photo: <input type="file" name="photo" /><br>
    <input type="submit">
   </form>
  </body>
</html>

Thanks in advance.

EDIT: It works! Kind of... Here's my new script:

<?php
if(isset($_POST)) {
 ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);
 $name = $_POST["name"];
 $address = $_POST["address"];
 $photo = $_POST['photo'];
 mkdir("articles/$name", 0755, true);
 copy('/homepages/9/d773272021/htdocs/input.html', "/homepages/9/d773272021/htdocs/articles/$name/");
 rename("/homepages/9/d773272021/htdocs/articles/$name/input.html", "/homepages/9/d773272021/htdocs/articles/$name/$name.html");
 //XML Data In Documents
 $xmlString = "<?xml version='1.0' encoding='UTF-8'?>
    <name>$name</name>
    <address>$address</address>
    <photo>$photo</photo>
 ";
 //The above
 $dom = new DOMDocument;
 $dom->preserveWhiteSpace = TRUE;
 $dom->loadXML($xmlString);
 $dom->save("articles/$name.xml");
}
?>

The directory and XML file get created, which is great. However, the HTML doesn't get copied and therefore does no get named to its new name.

Here are the errors I'm getting, any help is appreciated:

Warning: copy(): The second argument to copy() function cannot be a directory in /homepages/9/d773272021/htdocs/output.php on line 10
Warning: rename(/homepages/9/d773272021/htdocs/articles/cancer/input.html,/homepages/9/d773272021/htdocs/articles/cancer/cancer.html): No such file or directory in /homepages/9/d773272021/htdocs/output.php on line 11
Warning: DOMDocument::loadXML(): Extra content at the end of the document in Entity, line: 3 in /homepages/9/d773272021/htdocs/output.php on line 21
J Hilbert
  • 1
  • 1

2 Answers2

1

First of all you won't get all error's because of the reporting -1, add the following to the top to get all proper erros;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

second point is that your xml string is not propperly formed. The 1.0 and UTF-8 are not inside the string use the following;

$xmlString = "<?xml version='1.0' encoding='UTF-8'?>
  <name>$name</name>
  <address>$address</address>
  <photo>$photo</photo>
";

Third you are checking for a submit value that should be sent via a post request but after a quick var dump of your post request there are no $_POST["submit"] values this is never been set so it wouldnt go into your if, that is another reason you wont get an error.

USE if(isset($_POST)) {, instead of if(isset($_POST['submit'])) {

and i think that new DOMDocument is a class so this should be invoked with the (). do this $dom = new DOMDocument(); instead of$dom = new DOMDocument;

After the tips i gave you it should work, it is not working with me because i dont have the folder you are trying to access. my errors with your code can be checked down below in the link

screenshot of debug

John
  • 1,837
  • 1
  • 8
  • 12
o elhajoui
  • 394
  • 4
  • 14
  • Check https://stackoverflow.com/questions/1945989/php-class-instantiation-to-use-or-not-to-use-the-parentheses for your last point. – Nigel Ren Feb 28 '19 at 20:57
  • Ah thank you @NigelRen, it has been a long time ago since i actively worked with php. Working with strong oop langs now.. – o elhajoui Feb 28 '19 at 22:02
  • Thanks so much, updated my original question and would love some more help! – J Hilbert Feb 28 '19 at 22:40
0

Try new 

new DOMDocument();

And activate

error_reporting(E_ALL);
Frank
  • 1,901
  • 20
  • 27