사용자가 파일을 다운로드한 후 삭제
사용자에게 파일을 보내는 데 사용합니다.
header('Content-type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
사용자가 이 파일을 다운로드한 후 삭제하려고 하는데 어떻게 해야 하나요?
편집: 제 시나리오는 이렇습니다. 사용자가 다운로드 버튼을 누르면 스크립트가 임시 zip 파일을 만들고 사용자가 다운로드한 다음 해당 임시 zip 파일이 삭제됩니다.
EDIT2: 좋습니다. 한 시간에 한 번 임시 파일을 정리하는 cron 작업을 실행하는 것이 가장 좋습니다.
EDIT3: 스크립트를 테스트했습니다.unlink
사용자가 다운로드를 취소하지 않는 한 작동합니다.사용자가 다운로드를 취소하면 zip 파일이 서버에 남아 있습니다.그래서 지금은 그것으로 충분합니다.:)
EDIT4: 와우!connection_aborted()
속임수를 썼습니다!
ignore_user_abort(true);
if (connection_aborted()) {
unlink($f);
}
이것은 사용자가 다운로드를 취소하더라도 파일을 삭제합니다.
unlink($filename);
파일이 삭제됩니다.
그것은 그것과 결합되어야 할 필요가 있습니다.unlink
사용자가 다운로드를 취소한 경우에도 계속 실행됩니다.
ignore_user_abort(true);
...
unlink($f);
항상 register_shutdown_function을 사용하여 다음 솔루션을 사용합니다.
register_shutdown_function('unlink', $file);
사용자가 파일을 완전히 다운로드했는지 여부를 탐지하는 올바른 방법은 없습니다.
따라서 일정 시간 동안 사용하지 않으면 파일을 삭제하는 것이 가장 좋습니다.
connection_aborted()
나를 위해 일한 적이 없습니다.비록 ~일지라도ob_clean()
정확하게 작동합니다.이것이 다른 사람들에게도 도움이 되기를 바랍니다.
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
ob_clean();
flush();
if (readfile($file))
{
unlink($file);
}
저는 저에게 맞는 것을 찾을 수 없어서 이것을 생각해 냈습니다. 저에게는 잘 맞는 것 같습니다.
header('Content-type: application/zip'); //this could be a different header
header('Content-Disposition: attachment; filename="'.$zipName.'"');
ignore_user_abort(true);
$context = stream_context_create();
$file = fopen($zipName, 'rb', FALSE, $context);
while(!feof($file))
{
echo stream_get_contents($file, 2014);
}
fclose($file);
flush();
if (file_exists($zipName)) {
unlink( $zipName );
}
그게 누군가에게 도움이 되길 바랍니다.
저도 제 웹사이트 중 하나에서 매우 유사한 기능을 가지고 있습니다.이것은 다운로드/취소 후 임의로 생성된 폴더 및 zip 파일을 삭제하는 것과 같습니다.제가 여기서 설명해 드리자면, 누군가 유용하다고 생각할 수도 있습니다.
skin.skin:
이 페이지에는 "http://mysite.com/downoload/bluetheme "과 같은 다운로드 링크가 포함되어 있습니다.
.htaccess:
다운로드 요청을 php 파일로 리디렉션하기 위해 다음과 같은 규칙이 있습니다.[계속]
RewriteRule ^download/([A-Za-z0-9]+)$ download.php?file=$1 [L]
다운로드.php:
include "class.snippets.php";
$sn=new snippets();
$theme=$_GET['file'];
$file=$sn->create_zip($theme);
$path="skins/tmp/$file/$file.zip";
$config_file="skins/tmp/$file/xconfig.php";
$dir="skins/tmp/$file";
$file.=".zip";
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$file");
header("Pragma: no-cache");
header("Expires: 0");
readfile($path);
//remove file after download
unlink($path);
unlink($config_file);
rmdir($dir);
다운로드를 요청합니다.php는 스니펫 클래스를 사용하여 임의의 이름으로 디렉터리를 만듭니다.디렉토리 안에 zip 파일이 생성됩니다. 요청을 다운로드/취소한 후 모든 파일과 디렉토리가 삭제됩니다.
"파일 시작 시 추가 바이트" 문제에 직면한 사용자는 다음 게시물에서 제안한 대로 파일 읽기 전에 버퍼 정리 기능을 추가하는 것이 좋습니다.
https://stackoverflow.com/a/51083411/16381972
@ob_start(''); //@ supresses a warning
//header entries
ob_end_clean();
ob_clean();
readfile($file);
언급URL : https://stackoverflow.com/questions/2641667/deleting-a-file-after-user-download-it
'programing' 카테고리의 다른 글
도구를 사용하는 방법:build.gradle 파일에서 라이브러리를 재정의합니까? (0) | 2023.08.15 |
---|---|
기본 재배치 후 분기로 푸시할 수 없음 (0) | 2023.08.15 |
Visual Studio Code의 나란히 있는 파일에서 'git diff'를 보려면 어떻게 해야 합니까? (0) | 2023.08.15 |
안드로이드에서 텍스트 보기에 새 줄을 추가하려면 어떻게 해야 합니까? (0) | 2023.08.15 |
데이터 테이블을 Excel 워크시트에 덤프하는 속도를 높이는 방법은 무엇입니까? (0) | 2023.08.15 |