Alex
Alex

Reputation: 780

Can't create an HTML file with PHP

I'm trying to create an .html file using PHP, and filling it with the content of an array, but I can't create the html file. Here's my function:

function viewPag(){
    session_start();

    $handler=fopen("view.php", 'w+');

    for ($i=0; $i < count($_SESSION["Text"]); $i++) { 
        fwrite($handler,$_SESSION["Text"][$i]);
    }

    fclose($handler);

    header("Location: view.php");
}

Upvotes: 0

Views: 237

Answers (2)

Sriraman
Sriraman

Reputation: 7947

You can't run PHP inside .html file. But, You can do it by editing .htaccess file.

Save the file with .php extension and edit the .htaccess file as following.

AddType application/x-httpd-php .html

If you want to use it in only one page you can use the following code.

<Files yourpage.html>
AddType application/x-httpd-php .html
</Files>

Upvotes: 1

Carlos Campderr&#243;s
Carlos Campderr&#243;s

Reputation: 23002

It's possible that you are not on the folder you think. use echo getcwd(); to print the current folder, and then use a relative path to store the file where you want.

It also may be that your fopen call is failing due to permission issues. Add an if to test the $handler value after the fopen call:

if (!$handler) {
    echo "Could not open file for writing. Aborting";
    die();
}

And one thing you should really do is look at your logs and search for errors. Withouth looking at the logs you (and we) are just blind and don't know what is failing (maybe what fails is something before this function... we can't know for sure).

Upvotes: 0

Related Questions