php 에서 파일다운로드하는 방법은 다음과같이 Header 를 이용하여야 한다.
오늘 삽질을 좀 했는데 계속 헤더 앞에 다른 문자가 출력되어있다는 에러가 나는 것이다...
도데체 뭘까... 한 시간의 삽질 끝에... <? <- 요거 앞에 한칸의 공백이 있는 것을 발견했다....
Header 를 사용할 때는 그 앞에 어떤 것도 출력되면 안된다.
이 소스를 개발할 때는 이 것을 주의해야한다.
꼼꼼하게 앞에 빈칸이 없도록 해야할 것을 잊지말자.
<?
class filemodel extends model
{
function get_mode()
{
$file_id = $this -> get_check("file_id", "plus");
$file = mysql::singleton("file") -> get_file_name($file_id);
$file_url = $file['file_url'];
$file_name = $file['file_name'];
$f_name = basename($file_name);
$downloadPath = $file_url;
Header("Content-Type: file/unknown");
Header("Content-Disposition: attachment; filename=". $f_name);
Header("Content-Length: ".filesize("$downloadPath"));
header("Content-Transfer-Encoding: binary ");
Header("Pragma: no-cache");
Header("Expires: 0");
flush();
if ($fp = fopen("$downloadPath", "r")) {
print fread($fp, filesize("$downloadPath"));
}
fclose($fp);
}
}
?>
참고로 링크는 이런식으로 하면 되겠지?
<a href="/api/index.php?model=file&mode=get&file_id=<?=$value['file_id1']?>&view=script" ><?=$value['file1']['file_name'];?></a>
화이링~^^
오늘 삽질을 좀 했는데 계속 헤더 앞에 다른 문자가 출력되어있다는 에러가 나는 것이다...
도데체 뭘까... 한 시간의 삽질 끝에... <? <- 요거 앞에 한칸의 공백이 있는 것을 발견했다....
Header 를 사용할 때는 그 앞에 어떤 것도 출력되면 안된다.
이 소스를 개발할 때는 이 것을 주의해야한다.
꼼꼼하게 앞에 빈칸이 없도록 해야할 것을 잊지말자.
<?
class filemodel extends model
{
function get_mode()
{
$file_id = $this -> get_check("file_id", "plus");
$file = mysql::singleton("file") -> get_file_name($file_id);
$file_url = $file['file_url'];
$file_name = $file['file_name'];
$f_name = basename($file_name);
$downloadPath = $file_url;
Header("Content-Type: file/unknown");
Header("Content-Disposition: attachment; filename=". $f_name);
Header("Content-Length: ".filesize("$downloadPath"));
header("Content-Transfer-Encoding: binary ");
Header("Pragma: no-cache");
Header("Expires: 0");
flush();
if ($fp = fopen("$downloadPath", "r")) {
print fread($fp, filesize("$downloadPath"));
}
fclose($fp);
}
}
?>
참고로 링크는 이런식으로 하면 되겠지?
<a href="/api/index.php?model=file&mode=get&file_id=<?=$value['file_id1']?>&view=script" ><?=$value['file1']['file_name'];?></a>
화이링~^^
이올린에 북마크하기
