[FIXED] Include < and > symbols in .html file

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
MegasXLR
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Dec 06, 2018 4:25 am
Location: Sofia, Bulgaria
Contact:

Thu Dec 06, 2018 4:32 am

Hello everyone,
I'm new to PHP and the forum so sorry if this is a stupid question but here goes.

I want to create HTML elements from my PHP code and I managed to do what I want with one small problem.

Code: Select all

	$uploadfile = basename($_FILES["userfile"]["name"]);
	$count = 1;
	$html = file_get_contents("main.html");
	libxml_use_internal_errors(true);
	$doc = new DOMDocument();
	$doc->loadHTML($html);
	$divinfo = '<img src = "uploads/' . $uploadfile . '" id = "' . $count .'">';
	$body = $doc->getElementsByTagName("body")->item(0);
	$divtoadd = $doc->createElement("div", $divinfo);
	$body->appendChild($divtoadd);
	$doc->saveHTMLFile("main.html");
	$count++;
The code above aims to create a 'div' element at the end of the html file so that when I upload a picture to the page it would automatically show up where it should.
My problem is that the '<' and '>' symbols don't get written as they are but as &alt; and forgot the other one's code but you get the idea. Is there a way to write those symbols correctly so that my html page would read that it's an image tag and display the image?


Fixed it by doing this:

Code: Select all

	$htmlfile = file_get_contents("main.html");
	libxml_use_internal_errors(true);
	$doc = new DOMDocument();
	$doc->loadHTML($htmlfile);
	$imginfo = "uploads/" . $uploadfile;
	$divtoaddto = $doc->getElementById("uploadedPictures");
	$divtoadd = $doc->createElement("div");
	$divtoadd->setAttribute("class", "uploadedPic");
	$divtoaddto->appendChild($divtoadd);
	$imgtoadd = $doc->createElement("img", $imginfo);
	$imgtoadd->setAttribute("src", $imginfo);
	$imgtoadd->setAttribute("id", "img" . $count);
	$divtoadd->appendChild($imgtoadd);
I love rock/metal/pop but don't mind any other music genre except чалга...that thing sux
I also love cars

PS I'm friendly

Post Reply