user5581330
user5581330

Reputation:

Create an HTML file wih PHP?

I want to create an html file from a php file that will contain this code:

<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>

What i have so far:

$qq = "/"";
$myfile = fopen("mlgtest.html", "w")or die("Unable to open file!");
$txt = "<html>\n";
fwrite($myfile, $txt);
$txt = "<head>\n";
fwrite($myfile, $txt);
$txt = "<meta http-equiv=" + $qq + "refresh" + $qq + "content=" + $qq + "0; url="+ $qq + "http://example.com/" + $qq + ">/n";
fwrite($myfile, $txt);
$txt = "</head\n";
fwrite($myfile, $txt);
$txt = "</html>\n";
fwrite($myfile, $txt);
fclose($myfile);

But it doesn't write anything to the file.

Upvotes: 0

Views: 94

Answers (5)

RiccardoC
RiccardoC

Reputation: 866

The first row is wrong, and the concatenation of string in PHP is done with the dot (".")

$qq = "/";
$myfile = fopen("mlgtest.html", "w")or die("Unable to open file!");
$txt = "<html>\n";
$txt .= "<head>\n";
$txt .= "<meta http-equiv=" . $qq . "refresh" . $qq . "content=" . $qq . "0; url=". $qq . "http://example.com/" . $qq . ">/n";
$txt .= "</head>\n";
$txt .= "</html>\n";
fwrite($myfile, $txt);
fclose($myfile);

Upvotes: 0

Justinjs
Justinjs

Reputation: 140

No need to use fwrite($myfile, $txt); again and again. try this instead

<?php

$txt .= "<head>";
$txt .= "</head>";
$txt .= "<body>";
$txt .= "</body>";
$txt .= "</html>";

echo $txt;exit;
?>

Upvotes: 1

StudioTime
StudioTime

Reputation: 24019

You don't need $qq

$myfile = fopen("mlgtest.html", "w")or die("Unable to open file!");
            $txt = "<html>\n";
            fwrite($myfile, $txt);
            $txt = "<head>\n";
            fwrite($myfile, $txt);
            $txt = '<meta http-equiv="refresh" content="0; url=http://example.com/" />/n'; // notice using ' not "
            fwrite($myfile, $txt);
            $txt = "</head>\n";
            fwrite($myfile, $txt);
            $txt = "</html>\n";
            fwrite($myfile, $txt);
            fclose($myfile);

Upvotes: 0

Mike Resoli
Mike Resoli

Reputation: 967

You can create a new HTML document like so:

<?php
$dom = new DOMDocument('1.0'); //Create new document with specified version number
echo $dom->saveHTML();        //Outputs the generated source code
?>

Adding elements:

<?php
$br = $dom->createElement('br'); //Create new <br> tag
$dom->appendChild($br); //Add the <br> tag to document
?>

Upvotes: 0

Ashish Choudhary
Ashish Choudhary

Reputation: 2034

You have an extra double quote in $qq = "/"";. It should be $qq = "/";.

Also PHP use . for concatination and not +.

Why dont you simply use

$htmlContent = '<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/" />
</head>
<body>
</body>
</html>'

Upvotes: 1

Related Questions