0

送信ボタンを使用してページから特定の画像をダウンロードする必要があるため、フォームと php 送信アクションを使用しています。ここで私の html と php コード

HTMLコード

<form name="logo" action="" method="post">
<input type="hidden" name="lgpath" value="images/logo/<?php echo $SelPro['lgpat'];?>">
    <img src="images/cmplogo/<?php echo $SelPro['lgpat'];?>">               
    <div><input type="submit" value="<?php echo $SelPro['lgname'];?>" name="dwnlg" id="dwnlg"></div></div>
    </form>

PHPコード

<?php
    if($_POST['dwnlg'])
    {
        $LgPath=$_POST['lgpath'];
        header('Content-Disposition: attachment; filename="'.$LgPath.'"');
    }
?>

画像をヘッダーにダウンロードするパスを指定しましたが、現在のページをファイルとしてダウンロードするだけです。どうすれば解決できますか?

4

2 に答える 2

2

次のコードを試してダウンロードしてください

<?php 
    if($_POST['dwnlg'])
    {
        $LgPath=$_POST['lgpath'];
        header("Content-type: image/jpeg");  
        header('Content-Disposition: attachment; filename="'.$LgPath.'"');  
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header("Cache-Control: public");
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($LgPath));
        readfile($LgPath);  
     }
?>  

コンテンツ タイプ:

         if your image is in png format then it will be :
         header("Content-type: image/png");
         if your image is in jpeg format then it will be :
         header("Content-type: image/jpeg");
于 2012-06-27T06:50:53.640 に答える
0

ヘッダーを設定することは、データが何であるか、そしてそれをどうするかをクライアントに伝えるだけで、データを送信しません。

readfileを使用してそれを行うことができます

<?php
    if($_POST['dwnlg'])
    {
        $LgPath=$_POST['lgpath'];
        header('Content-Disposition: attachment; filename="'.$LgPath.'"');
        readfile($LgPath);
    }
?>
于 2012-06-27T06:51:15.617 に答える