1

アップロード先のディレクトリにすべての画像を表示するために正常に動作する PHP スクリプトがあります。誰かがボタンをクリックして画像をダウンロードできるように、小さなダウンロードボタンを作成したいと思います。人々が私たちのロゴをダウンロードできるように、私は自分の会社のためにこれを作成しています。

<?php
        // Find all files in that folder
        $files = glob('grips/*');

        // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
        natcasesort($files);


        // Display images
        foreach($files as $file) {
           echo '<img src="' . $file . '" />';
        }

    ?>

ボタンを作成して href を呼び出すだけでよいと思います$fileが、それはファイルにリンクして画像を表示するだけです。自動ダウンロードするかどうかはわかりません。どんな助けでも素晴らしいでしょう。

4

1 に答える 1

0

ファイルにいくつかのヘッダーを追加するだけで、download.php次のようにファイルを読み取ることができます。

ファイルに送信されるデータを必ずサニタイズしてください。他の人があなたの php ファイルをダウンロードできないようにしてください。

<?php
    // Find all files in that folder
    $files = glob('grips/*');

    // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
    natcasesort($files);


    // Display images
    foreach($files as $file) {
       echo '<img src="' . $file . '" /><br /><a href="/download.php?file='.base64_encode($file).'">Download Image</a>';
    }

?>

ダウンロード.php

$filename = base64_decode($_GET["file"]);

// Data sanitization goes here
if(!getimagesize($filename) || !is_file($filename)){
    // Not an image, or file doesn't exist. Redirect user
    header("Location: /back_to_images.php");
    exit;
}

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Disposition: attachment; filename=".basename($filename).";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($filename)); 

readfile($filename); 
于 2013-05-29T18:18:32.853 に答える