简体   繁体   中英

Displaying images with php from the URL

Well, I've been on nearly all the posts regarding this issue. I still can't seem to get this working.

I'm basically following this:

http://www.w3schools.com/php/php_file_upload.asp

I've got it all working and even managed to get the uploaded picture to display on a php page.

The problem is really, is that I'd like it so that it wouldn't just display the last picture you uploaded, but instead any picture that is in the "uploads" folder.

I'd like to do this in a separate php from the tutorial and the URL would look something like this:

http://myexample.com/display.php?pic=ImAPicture

This way, all the pictures in the uploads folder can be displayed as long as you have the name.

I've played about with it, but I can't seem to get it to work.

Please refer to the tutorial to know roughly how I'm working with it.


To Sharky, I am unable to reply. I tried your code but I got an error

Parse error: syntax error, unexpected '}' in /websites/123reg/LinuxPackage21/fl/an/de/flanderskiller.com/public_html/display.php on line 12

I deleted the }

and then I got a new error

Parse error: syntax error, unexpected T_IF in /websites/123reg/LinuxPackage21/fl/an/de/flanderskiller.com/public_html/display.php on line 14

Please help. Thank you!

You need to read your image and echo it with header Content-type: image/jpeg or image/png like this:

<?php
    $image = file_get_contents("path/to/image.jpg");
    header("Content-type: image/jpeg");
    echo $image;
?>

your display.php

<?php

    $uploads_folder = "/home/yourUnixUsername/public_html/uploads/";        
    $web_folder = "/uploads/";

    $image_requested = $_GET['pic'];  // filename from url

    $images = array();

    if(file_exists($uploads_folder.$image_requested)) // if that image exists, get every image from that folder
    {

        if ($dh = opendir($uploads_folder))
        {
        $inc = 0;

            while (($file = readdir($dh)) !== false)
            {
                if (!is_dir($uploads_folder.$file))
                {
                    $images[$inc]=$web_folder.$file; // construct a web path to image
                    $inc++;
                 }
             }

             closedir($dh);

         }
    }

    foreach($images as $an_image)
    {
        echo '<img src="'.$an_image.'">';
    }
?>

not been able to test this but i think it will work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM