dongyin4202 2015-06-24 09:23
浏览 39
已采纳

Android:图像无法从服务器下载

I have uploaded few images on my server which will be downloaded to my android application by passing a url. I have written a php file which displays the image. I pass the URL to my android application like this :

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

When I copy paste the URL directly in browser the image is displayed correctly. However the image file is not getting downloaded on my android application. I know that android code is correct because when I am giving any other random image URL the image is downloading correctly. Do I have to give something else in my php file to make the image 'downloadable'.

Image.php

<?php

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

 ?>     
  • 写回答

1条回答 默认 最新

  • dsjswclzh40259075 2015-06-24 10:26
    关注

    It seems you want the PHP to output the image directly. Instead, your code is generating an HTML with the image on it. Although your browser displays similar results, the underlying content is different.

    What you actually need could be this:

    <?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();
    }
    

    This is vulnerable to hazardous $_REQUEST['image'] input. Just filter it somehow. Also you must generate the correct $ctype for the image file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?