通过<a download=""> where href contains a php variable which holds the content of the image

时间:2018-11-18 09:15:08

标签: php html html5

Here's the snippet of my code where $arr[$i]['notes'] stores the content of the image retrieved from the database

<div class="subs">


        <?php
            echo '<table style="width:100%">';
            echo '<th>Subject Code</th>';
            echo '<th>Subject Title</th>';
            echo '<th>Semester</th>';
            echo '<th>Credits</th>';
            echo '<th>Faculty</th>';
            echo '<th>Notes</th>';
                for($i=0;$i<sizeof($arr);$i++)
                {
                    if(empty($fac[$i])){
                        $fac[$i]="--";

                    }
                    echo '<tr><td>'.$arr[$i]['subcode'].'</td><td>'.$arr[$i]['subtitle'].'</td><td>'.$arr[$i]['sem'].'</td><td>'.$arr[$i]['credits'].'</td><td>'.$fac[$i].'</td><td> <a download="'.$arr[$i]['subcode'].'.pdf" href="$arr[$i][\'notes\']">'.$arr[$i]['subcode'].' Notes</a></td></tr>';

                }

                echo '</table>';
                echo '<br />';
        ?>




    </div>

The link is appearing on the page ,but when i click on it, i am getting "Failed-No file' error.Is the right of specifying the php variable as the source the tag. Is there any other this can be done?

Screen shot of the error

下载图像

1 个答案:

答案 0 :(得分:0)

如果您知道使用数据方案和base64编码的mime类型,则可以从文件内容中下载图像。

<html>
<head>

</head>
<body>

    <?php 

    $mime_type = "image/png";
    $file_content = file_get_contents("img.png");         

    ?>
    <a href="data:<?php echo $mime_type; ?>;base64,<?php echo base64_encode($file_content); ?>" download="anything.png">Download</a>

</body>