내가 원하는 파일을 다운로드할 수 있도록 하는 웹 페이지를 방문하지 빛위에 사용자가 PHP. 하지만 잘 모르시는 것 같은데 어떻게 관련이 있다 ',' file_get_contents 키워봤지 실행할 수 있습니다.
$url = "http://example.com/go.exe";
파일 다운로드 후 헤더입니다 (위치) '와' 다른 페이지로 리디렉션하는 아니다. 그냥 중됩니다.
대한 문서를 읽을 내장현 PHP 함수 [참고: 윈도우즈에서 동작하지 않습니다.] (http://php.net/manual/en/function.readfile.php)
$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url); // do the double-download-dance (dirty but worky)
또한 응용 프로그램, 응용 프로그램 추가 / zip 파일 유형에 따라 smartupdate 이벤트수정적절한 컨텐츠 / pdf 등 - 단지 트리거하여 않으려면 다른 이름으로 저장 대화.
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"file.exe\"");
echo readfile($url);
올바른지
header("Location: $url");
<?php
$file = "http://example.com/go.exe";
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"". basename($file) ."\"");
readfile ($file);
exit();
?>
때 아닌 브라우저를 사용하여 파일을 열 수 있는, 또는 그냥 '위치' 헤더입니다 사용할 수 있습니다.
<?php header("Location: http://example.com/go.exe"); ?>
해당 파일을 먼저 url 로 표시 및 그 값을 설정합니다.
이네스.프프
<a href="download.php?download='.$row['file'].'" title="Download File">
다운로d.프프
<?php
/*db connectors*/
include('dbconfig.php');
/*function to set your files*/
function output_file($file, $name, $mime_type='')
{
if(!is_readable($file)) die('File not found or inaccessible!');
$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"jpg" => "image/jpg",
"php" => "text/plain",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"pdf" => "application/pdf",
"txt" => "text/plain",
"html"=> "text/html",
"png" => "image/png",
"jpeg"=> "image/jpg"
);
if($mime_type==''){
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
};
@ob_end_clean();
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$size;
header("Content-Length: ".$size);
}
$chunksize = 1*(1024*1024);
$bytes_send = 0;
if ($file = fopen($file, 'r'))
{
if(isset($_SERVER['HTTP_RANGE']))
fseek($file, $range);
while(!feof($file) &&
(!connection_aborted()) &&
($bytes_send<$new_length)
)
{
$buffer = fread($file, $chunksize);
echo($buffer);
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else
die('Error - can not open file.');
die();
}
set_time_limit(0);
/*set your folder*/
$file_path='uploads/'."your file";
/*output must be folder/yourfile*/
output_file($file_path, ''."your file".'', $row['type']);
/*back to index.php while downloading*/
header('Location:index.php');
?>
못한 경우 메모리 한계를 넘는 크기를 가진 파일을 다운로드할 수 있는 ' (' memory_limit 'ini 설정), PHP 치명적 오류가 발생할 것이다. 메모리 크기가 허용됩니까 5242880 바이트입니다 모두 '오류, 이 작업을 수행할 수 있습니다.
// File to download.
$file = '/path/to/file';
// Maximum size of chunks (in bytes).
$maxRead = 1 * 1024 * 1024; // 1MB
// Give a nice name to your download.
$fileName = 'download_file.txt';
// Open a file in read mode.
$fh = fopen($file, 'r');
// These headers will force download on browser,
// and set the custom file name for the download, respectively.
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
// Run this until we have read the whole file.
// feof (eof means "end of file") returns `true` when the handler
// has reached the end of file.
while (!feof($fh)) {
// Read and output the next chunk.
echo fread($fh, $maxRead);
// Flush the output buffer to free memory.
ob_flush();
}
// Exit to make sure not to output anything else.
exit;
Mime type 에 대한 수정, 이 역시 수락됨 그들위에 오토메이티드 탐지합니다 런타임용으로 개발하십시오:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: '.finfo_file($finfo, $path));
$finfo = finfo_open(FILEINFO_MIME_ENCODING);
header('Content-Transfer-Encoding: '.finfo_file($finfo, $path));
header('Content-disposition: attachment; filename="'.basename($path).'"');
readfile($path); // do the double-download-dance (dirty but worky)
다음 코드는 올바른 방법을 설명한 대로 미리 다운로드 서비스를 구현하는 다음 자습서는
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=\"$file_name\"");
set_time_limit(0);
$file = @fopen($filePath, "rb");
while(!feof($file)) {
print(@fread($file, 1024*8));
ob_flush();
flush();
}
http://php.net/manual/en/function.readfile.php
php <,?
if (file_exists ($ file) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
>,?
너무 크게 적은 자원을 소비할 수 있는 다운로드하십시오 스트리밍할 수 있습니다. 예:
$readableStream = fopen('test.zip', 'rb');
$writableStream = fopen('php://output', 'wb');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="test.zip"');
stream_copy_to_stream($readableStream, $writableStream);
ob_flush();
flush();
위의 예에서 실로나는 테스트리치프 다운로드, 즉 실제로는 안드로이드 스튜디오 zip 내 로컬 컴퓨터의). 읽기 전용 php://output 은 스트림 (일반적으로 사용되는 에코 또는 인쇄). 그 후, 그냥 헤더도 (소스, 대상), 콜 stream_copy_to_stream 설정해야 합니다. 파이프 역할을 담당하는 stream_copy_to_stream () 메서드를 입력 스트림 파이프 redhat. 스트림 소스에서 (읽기 스트리밍합니다) 와 대상 (쓰기 스트림).