Help with adding php to an htm page

When I add this to an htm page:

$file = '/home/me/www/images/' . $row['filename'];
if(file_exists($file)) {
$file = $row['filename'];
} else {
$file = 'default.jpg';
echo '<img src="' . $file . '" alt="No image" />';

I only see this part on the htm page:
'; ?>

Can you help me with this?

You have two basic options:

  1. change the file extension from .htm to .php, or
  2. tell your server to parse PHP code in .htm files. E.g. add this to a .htaccess file: AddType application/x-httpd-php .html .htm

Thanks for your reply. It is a templated php script that I’m using with a php piece and an htm piece for each displayed page. I took your suggestion and added “AddType application/x-httpd-php .html .htm” to .htaccess, but I still see the '; ?>
If I add the <? php code posted above, to the php piece of the page, what can I use on the htm page, corresponding with the code, to show a link to the image? I’d like a text link, like >Click Here<
Any additional help will be appreciated.

I think you need to state more clearly what you are trying to do. What would you like people to see in their browser? Do you want the image to appear on a web page? Do you want to create a link to an image on a web page? It’s really not clear.

This is also quite vague:

a php piece and an htm piece for each displayed page

Remember that the browser just gets an HTML page. Any PHP on that page will get processed on the server, just leaving HTML that is sent to the browser. That’s why I’m asking what you are actually trying to achieve.

Thanks for your reply.
I’d like to “create a link to an image on a web page”.

O well, perhaps change your echo statement to something like this:

echo '<a href="' . $file . '">View Image</a>';

So why are you adding PHP code to the template, instead of to the page that uses the template?

Can you please give me an example of what you mean?

Try this:

$file = "/home/me/www/images/". $row['filename'];
if(file_exists($file)) {
$file = $row["filename"];
} else {
$file = "default.jpg";
<img src="<?php echo $file; ?> " alt="No image" />

and to run PHP codes the page must be named as .php .htm wont be detected as php pages.

unless you tell the server to parse all .htm files for PHP