Jain Rachit Kumar
Jain Rachit Kumar

Reputation: 4277

Unable to open file via PHP from HTML page

Using the example available at http://www.w3schools.com/php/php_file.asp , i am learning php with html . While trying the provided example to open a file on server using php by hitting an html file on browser , i see that no file is getting created on the server and it seems php part isn't running there . below is the exact file that i using

<html>
<body>

<?php
$file=fopen("welcome.txt","w+");
fwrite($file, "test");
?>

</body>
</html>

Below are apache details,
[root@ learneg]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Feb  7 2012 09:50:11

I tried some other codes too , but no success . Please suggest what i could be missing here

Upvotes: 0

Views: 155

Answers (2)

Jens
Jens

Reputation: 1154

There should be a simple explanation for this.

You are running it on apache.

1) Is php5 installed on apache ?

2) Are you sure the php5 module is enabled ?

3) Does the file have a .php extension ?

Apache only parses php when it sees the .php extension.

You could also try this:

$file_contents = fopen( "<yourfile>", "r" );
print $file_contents; // This prints the output if it can find it.
fclose($file_contents);
// Write it to a another file.

Upvotes: 0

michal.hubczyk
michal.hubczyk

Reputation: 697

You need to configure Apache, so it will parse also .html file. Find section with AddType in httpd.conf and add/uncomment following:

AddType application/x-httpd-php .htm

AddType application/x-httpd-php .html

Upvotes: 1

Related Questions