簡體   English   中英

Android:圖像未從服務器下載

[英]Android: Images not downloading from server

我在我的服務器上上傳了幾張圖片,這些圖片將通過傳遞url下載到我的 android 應用程序。 我寫了一個顯示圖像的php文件。 我將URL傳遞給我的 android 應用程序,如下所示:

' http://myURL/getImage.php?image=logo.png '。

當我直接在瀏覽器中復制粘貼URL ,圖像顯示正確。 但是,圖像文件沒有下載到我的 android 應用程序上。 我知道 android 代碼是正確的,因為當我提供任何其他隨機圖像URL ,圖像正在正確下載。 我是否必須在我的 php 文件中提供其他內容才能使圖像“可下載”。

圖片.php

<?php

  echo '<img src="Images/'.$_REQUEST['image'].'"/><br />';

 ?>     

看來您希望 PHP直接輸出圖像 相反,您的代碼正在生成帶有圖像的 HTML 盡管您的瀏覽器顯示類似的結果,但底層內容不同。

你真正需要的可能是這樣的:

<?php

$filepath = 'Images/'.$_REQUEST['image'];
$filename = basename($filepath);
$ctype = 'image/jpeg'; // assuming it is a .jpg file

if (is_file($filepath)) {
    header('Content-Type: '.$ctype);
    header('Content-Length: ' . filesize($filepath));
    header('Content-Disposition: attachment; filename="'.$fileName.'"');
    echo file_get_contents($file);
    exit();
} else {
    header("HTTP/1.0 404 Not Found");
    // you may add some other message here
    exit();
}

這很容易受到危險的$_REQUEST['image']輸入的影響。 以某種方式過濾它。 此外,您必須為圖像文件生成正確的$ctype

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM