【问题标题】:Why can't downloaded image be opened?为什么下载的图片打不开?
【发布时间】:2021-03-06 07:45:37
【问题描述】:

我在 Wordpress 中使用 ACF,并用它创建了一个图片库。每个图像都应该有一个下载链接,以便在单击时将图像下载到用户计算机。但是,当我尝试下载时,它似乎可以下载,但是图像无法打开。相反,我收到一条错误消息,指出图像无法打开并且可能已损坏或使用了无法识别的文件格式(图像确实以 jpg 格式下载)。 这是我创建的下载链接。

 <a href="<?php get_stylesheet_directory().'/webservices/download.php?filename='.$media_gallery_image['image_download_link']; ?>"
                           title="click to download this image"
                           target="_blank"
                           class="js-download"
                           download="<?php echo $media_gallery_image['image_download_link']; ?>"><?php echo $translated[$lang]['download_text']; ?></a>

这是我的 download.php 文件。

<?php

$file = $_GET['file'];

download_file($file);

函数下载文件($fullPath){

// Must be fresh start
if( headers_sent() )
    die('Headers Sent');

// Required for some browsers
if(ini_get('zlib.output_compression'))
    ini_set('zlib.output_compression', 'Off');

// File Exists?
if( file_exists($fullPath) ){
    // Parse Info / Get Extension
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);

    // Determine Content Type
    switch ($ext) {
        case "gif": $ctype="image/gif"; break;
        case "png": $ctype="image/png"; break;
        case "jpeg":
        case "jpg": $ctype="image/jpg"; break;
        default: $ctype="application/force-download";
    }

    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$fsize);
    ob_clean();
    flush();
    readfile( $fullPath );

} else
    die('File Not Found');

}

我无法弄清楚为什么这不起作用。有人可以告诉我我做错了什么吗?提前谢谢你。

【问题讨论】:

  • 用文本或十六进制编辑器打开下载的文件,并检查其中是否有任何 PHP 错误消息,或其他实际上不属于图像文件的输出。
  • 好主意...这样做,我发现它根本没有下载图像。相反,它正在下载 HTML 页面。不知道为什么,但这确实给了我一个开始寻找的地方。
  • (这甚至需要一个特殊的脚本吗?如果图像不是来自不同的来源,那么带有download 属性集的链接可能已经可以了。developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes
  • 这是我的第一次尝试,但由于某种原因它不起作用。失败 - 无文件错误。

标签: php wordpress advanced-custom-fields custom-wordpress-pages


【解决方案1】:

看起来我试图让它过于复杂......终于让它工作了。其他任何人都想弄清楚这一点,这对我有用。这是我的 download.php 文件。

    <?php
if ( $_SERVER['REQUEST_METHOD'] === 'GET' ) {
    if ( !is_null( $_GET['filename'] ) && !empty( $_GET['filename'] ) ) {
        $file = $_GET['filename'];
        $fname = basename( $file );
        try {
            header( 'Content-type: image/*' );
            header( "Content-Disposition: attachment; filename=\"$fname\"" );
            readfile( $file );
        } catch ( Exception $ex ) {
            echo $ex->getMessage();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2021-09-26
    • 2020-02-25
    • 2021-02-23
    相关资源
    最近更新 更多