如何从文件夹php中显示图片

I have a folder where uplaoded files are saved. This page is used to retrieve those saved files. A picture called "flower.jpeg" is in the folder, and I am trying to display the picture using an image tag, as shown below. I have two options, but none of them is working.

  <!DOCTYPE html>
    <html>
    <body>
    <?php

        $picture=flower.jpeg
        $playfile='/upload/$picture';

    ?>
        // <img href= '<?php $playfile ?>'  width="800" height="600">   
        // <img src= '<?php echo $playfile; ?>'  width="800" height="600">  <br>

    <script>
        document.write('<a href="' + document.referrer + '">Go Back To Chat</a>');
    </script>
    </body>
    </html>

flower.jpeg requires quotes and the $playfile assignment is not properly specified as, to my knowledge, strings within single quotes are not parsed for variables within PHP.

$picture='flower.jpeg';
$playfile='/upload/'.$picture;

Additionally correct use of the tag is as follows

<img src="<?php echo $playfile; ?>"  width="800" height="600">

As the attributes need enclosing by double-quotes, you are just using PHP to echo out a variable containing a string.

An explanation of what is wrong. You're using the following code to build your link:

$picture=flower.jpeg
$playfile='/upload/$picture';

First off, you need to quote the picture

$picture = 'flower.jpg';
//or
$picture = "flower.jpg";

The next problem is how you're building the playfile. You need to exit single quotes to use a variable or use double quotes:

$playfile = '/upload/'.$picture;
//or
$playfile = "/upload/$picture";
//or even
$playfile = "/upload/".$picture;

The single quotes will take the text as is where the double quotes will evaluate variables in them. I suggest continuing to read up on the basics of PHP and strings.

Here are a few steps that may help you in finding if the path to the image (or the 'href' as you call it) is correct. No direct solution is possible as we do not have access to your file system, as Simon pointed out.

  • Check if the file extension of your picture is mentioned correctly. Check also that it is in the correct CAPS. For example, if you saved your picture using MS Paint, the extension will be *.PNG but your script may contain *.png; this may cause problems in some cases.

  • .jpg and .jpeg are two different extensions sometimes (in the sense of the above point). You may want to take that into consideration.

  • You may want to verify that the directory structure of the image is correct (i.e. verify that you are specifying the correct path to the file).

Hope this helped you in some way. The above steps are rough and vague. If they failed to help you, please comment, and someone will be able to help you.