PHP를 사용하여 강제 파일 다운로드
배경:
"다운로드"를 구현하려고 합니다. 이 파일' 기능을 사용하면 사용자가 별도의 서버에 저장된 비디오를 스트리밍하는 대신 직접 다운로드할 수 있습니다.
해결책:
PHP를 사용하여 강제로 파일을 다운로드하려면 다음 코드 조각을 활용할 수 있습니다.
// Specify the file location and remote URL. $file_name = 'file.avi'; $file_url = 'http://www.myremoteserver.com/' . $file_name; // Set appropriate headers to force download. header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"\"" . $file_name . "\"\""); // Initiate the download process. readfile($file_url); exit; // Prevent any further script output.
구현 :
참고: readfile을 읽을 수 있도록 fopen_wrappers가 활성화되어 있는지 확인하세요. 원격 URL입니다.
위 내용은 PHP를 사용하여 파일을 강제로 다운로드하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!