ProgramingTip

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

bestdevel 2020. 12. 29. 07:44
반응형

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에서 테스트했습니다.


파일 다운로드시 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);

이 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();


문제는 내가 ajax를 사용하여 메시지를 서버에 게시했다는 것입니다. 직접 링크를 사용하여 파일을 다운로드하면 모든 것이 잘 작동했습니다.

대신이 다른 Stackoverflow Q & A 자료를 사용했습니다.


나를위한 일

$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.");
}

에 기반 인 Farhan Sahibole의 대답 :

        header('Content-Disposition: attachment; filename=Image.png');
        header('Content-Type: application/octet-stream'); // Downloading on Android might fail without this
        ob_clean();

        readfile($file);

이것이 작동하는 데 필요한 전부입니다. 나는 작동하는 데 필요하지 않은 것을 제거했습니다.

열쇠는 사용하는 것입니다 ob_clean();

참조 URL : https://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header

반응형