PHP:如何添加图片? [关闭]

The simplest question but I can't get it working... What's wrong with the way I'm trying to add an image to this php file?

<?php header("HTTP/1.0 404 Not Found"); ?>
<?php  defined('C5_EXECUTE') or die("Access Denied."); ?>

<h1 class="error"><?php echo t('Page Not Found')?></h1>

<?php echo t('We could not find a page at this address.')?>

<?php  if (is_object($c)) { ?>
    <br/><br/>
    <?php  $a = new Area("Main"); $a->display($c); ?>
<?php  } ?>

<?php
    echo "<img src="img.jpg">"
?>

<a href="<?php echo DIR_REL?>/"><?php echo t('Back to Home')?></a>.

The file named img.jpg sits in the same directory as this .php file. When it runs, I see this error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in line 21 where line 21 is echo "<img src="img.jpg">".

Two, or possibly three, things are wrong with the way you're adding an image.

  • You need to use different kinds of quotation marks (" vs '), otherwise they cancel each other out.
  • You need a ; to end the line in PHP
  • Your image path may be broken. If img.jpg is not in the same directory as the PHP script, it won't work.

Replace:

"<img src="img.jpg"/>"

with

"<img src='img.jpg'/>";

If the problem is with your image path, try using an absolute path (src="http://example.com/your/path/img.jpg") instead of the relative path (src="img.jpg"). If that works, then it means that the relative path was wrong.

instead of :

echo "<img src="img.jpg">";

you can do:

echo "<img src='img.jpg'>";

or

echo '<img src="img.jpg">';

or even escape the quote:

echo "<img src=\"img.jpg\">";

You have a wrong quotation on the img line and then I suggest keeping it all in PHP. You can replace your code with this code which is more easy to read and maintain:

header("HTTP/1.0 404 Not Found");
defined('C5_EXECUTE') or die("Access Denied.");
echo '<h1 class="error">'. t('Page Not Found') .'</h1>';
echo t('We could not find a page at this address.');

if (is_object($c)) {
    echo '<br /><br />';
    $a = new Area("Main");
    $a->display($c);
}

echo "<img src='img.jpg'>";
echo '<a href="'. DIR_REL .'/">'. t('Back to Home') .'</a>.';