繁体   English   中英

通过URL从php显示图像

[英]Displaying images with php from the URL

好吧,我几乎在所有有关此问题的帖子上。 我似乎仍然无法正常工作。

我基本上是这样的:

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

我已经完成所有工作,甚至设法将上传的图片显示在php页面上。

问题确实是,我想要它,这样它不仅会显示您上传的最后一张图片,而且会显示“上传”文件夹中的任何图片。

我想在本教程中的单独php中执行此操作,URL看起来像这样:

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

这样,只要您有名字,就可以显示上载文件夹中的所有图片。

我已经尝试过了,但是似乎无法使它正常工作。

请参考该教程以大致了解我的工作方式。


对于Sharky,我无法回复。 我尝试了您的代码,但出现错误

解析错误:语法错误,/ websites / 123reg / LinuxPackage21 / fl / an / de / flanderskiller.com / public_html / display.php在第12行出现意外

我删除了}

然后我得到一个新的错误

解析错误:语法错误,/ websites / 123reg / LinuxPackage21 / fl / an / de / flanderskiller.com / public_html / display.php中第14行出现意外的T_IF

请帮忙。 谢谢!

您需要阅读图像并使用标题Content-type:image / jpeg或image / png来回显图像,如下所示:

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

您的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.'">';
    }
?>

尚无法测试,但我认为它将起作用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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