0

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");
}
5
  • 3
    If the file is not created I suggest 1. you check the http servers error log file and 2. you implement error handling. Most likely you have a permission problem. Commented Dec 18, 2013 at 8:15
  • What do you mean that you can't create? What happens? Commented Dec 18, 2013 at 8:15
  • You are right arkascha, it was a permission problem... Commented Dec 18, 2013 at 8:19
  • @Doruko what filesystem / OS do you have? Commented Dec 18, 2013 at 8:25
  • I'm using Ubuntu Server right now Commented Dec 18, 2013 at 8:41

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.