0

画像をハードディスクにダウンロードするために以下のコードを使用しています。ニュースページに配置した最初のリンクを使用すると、画像をダウンロードできます。ギャラリーに配置した2番目のリンクは機能していません。パスは正しく、downlaod.php ファイルを使用せずに画像を開きました。パスにエラーはありませんが、画像はダウンロードされていません。別の画像IDを使用して別の画像を試しましたが、その特定のページでは機能しません

<a href="<?php echo SITE; ?>download.php?filename=<?php echo SITE; ?>uploads/news/<?php echo $rowimg['image']; ?>" title="download">

画像のダウンロード

 <a href="<?php echo SITE; ?>download.php?filename=<?php echo SITE; ? >uploads/gallery/<?php echo $row['folder']; ?>/<?php echo $row1['folder']; ?>/<?php echo  $row2['folder']; ?>/<?php echo $rowimg['image']; ?>" title="Download">
download</a> 

エラー: ファイルが壊れているか大きすぎる可能性があります

ダウンロード.php

<?php    
$filename = $_GET["filename"];
$buffer = file_get_contents($filename);
/* Force download dialog... */
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Type: application/image");

/* Don't allow caching... */
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

/* Set data type, size and filename */
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($buffer));
header("Content-Disposition: attachment; filename=$filename");

/* Send our file... */
echo $buffer; 
?> 
4

1 に答える 1

1

使用してくださいcURL

$ch = curl_init('http://my.image.url/photo.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // buffer output (`true` if you will be redirecting stream to the user, in $result will be your image content)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // because image is binary data

// Within these two lines you can donwload image directly to the file on your server
// $fp = fopen('/my/saved/image.file', 'w');
// curl_setopt($ch, CURLOPT_FILE, $fp);

$result = curl_exec($ch); // executing requrest (here will be raw image data if RETURNTRANSFER == true)
$response_code = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); // getting response code to ensure that request was successfull
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); // getting return content-type

// Listing allowed image types    
$allowed_mimes = array(
            'image/jpeg',
            'image/gif',
            'image/png'
            );

if ($response_code == 200 AND in_array($content_type, $allowed_mimes) ) {
  header( 'Content-Type: '.$content_type );
  echo $result; // echoing image to STDOUT
} else
  return false;
于 2012-07-27T08:36:06.033 に答える