簡體   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