복붙노트

header ()를 사용하여 PHP로 파일 다운로드 강제 실행

PHP

header ()를 사용하여 PHP로 파일 다운로드 강제 실행

사용자가 내 서버에있는 일부 파일을 다운로드 할 수 있기를 원하지만 인터넷에서이 많은 예제 중 하나를 사용하려고하면 아무 것도 작동하지 않습니다. 나는 다음과 같은 코드를 시도했다 :

<?php

$size = filesize("Image.png");

header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile("Image.png");

심지어 찾을 수있는 가장 기본적인 예제를 사용하려고했습니다.

<?php
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="Image.png"');
readfile('Image.png');

필자가 테스트 한 결과 다른 모든 코드가 제거되었으며이 코드로 빈 파일을 사용하여 외부 소스에서 생성 된 오류를 제거했습니다.

콘솔을 보면 파일이 올바른 헤더와 함께 전송됩니다.

'Content-Disposition: attachment; filename="Image.png"'

그러나 저장 대화 상자는 표시되지 않습니다.

또한 콘텐츠 처리 헤더에 첨부 대신 인라인으로 시도했지만 차이는 없습니다. Firefox 8.0.1 Chrome 15.0.874.121 및 Safari 5.1.1에서 테스트했습니다.

해결법

  1. ==============================

    1.파일 다운로드시 MIME 형식을 JPEG로 추가하지 않으셔도됩니다.

    파일 다운로드시 MIME 형식을 JPEG로 추가하지 않으셔도됩니다.

    header('Content-Type: image/png');
    

    이 헤더는 절대로 실패하지 않았습니다.

    $quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
    $size   = filesize($file);
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $quoted); 
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $size);
    
  2. ==============================

    2.이것은 나를 위해 PNG 및 PDF 다운로드에 대한 매력처럼 일했습니다.

    이것은 나를 위해 PNG 및 PDF 다운로드에 대한 매력처럼 일했습니다.

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$file_name.'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file_url)); //Absolute URL
    ob_clean();
    flush();
    readfile($file_url); //Absolute URL
    exit();
    
  3. ==============================

    3.문제는 아약스를 사용하여 서버에 메시지를 게시하는 것이 었습니다. 직접 링크를 사용하여 모든 파일이 제대로 작동 할 때 파일을 다운로드했습니다.

    문제는 아약스를 사용하여 서버에 메시지를 게시하는 것이 었습니다. 직접 링크를 사용하여 모든 파일이 제대로 작동 할 때 파일을 다운로드했습니다.

    나는이 다른 Stackoverflow Q & A 재료를 대신 사용했다.

  4. ==============================

    4.나를위한 그 일

    나를위한 그 일

    $attachment_location = "filePath";
    if (file_exists($attachment_location)) {
    
        header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
        header("Cache-Control: public"); // needed for internet explorer
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length:".filesize($attachment_location));
        header("Content-Disposition: attachment; filename=filePath");
        readfile($attachment_location);
        die();
    } else {
        die("Error: File not found.");
    }
    
  5. from https://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header by cc-by-sa and MIT license