php 에서 파일 다운로드 할 때 파일명 설정하기...

exsider의 이미지

php 게시판을 만드는데 파일 다운로드 기능을 구현하려고 합니다.

다운로드하는 url 은 download.php?id=숫자
형식이고 id 값을 이용하여 DB 에서 실제 파일명을 읽어와서 서버의
특정 디렉토리에 있는 파일을 전송합니다.

문제는 파일 전송은 잘되는데 웹브라우저에서 다운받을 때의 파일명이
download.php 가 된다는 것입니다.
저는 DB 에서 읽어온 파일명이 다운받을 때 기본 저장 파일명이 되게 하고 싶습니다.

제가 인터넷을 뒤져보니 header 설정을 다음처럼 하더군요.

    Header("Content-type: file/unknown");
    header("Content-Length: $file_size") ;
    Header("Content-Disposition: attachment; filename=$view_name") ;
    header("Content-Transfer-Encoding: binary") ;
    Header("Pragma: no-cache");
    Header("Expires: 0");

$file_size 는 전송할 파일의 크기이고 $view_name 은 브라우저에 보이게할 파일명입니다.
(서버에 실제 저장된 파일명과는 다릅니다.)
위 처럼 해봤지만 역시 브라우저에는 download.php 라는 이름으로 저장됩니다.
뭐가 문제일까요???
sjpark의 이미지

// 생략
header("Content-type: $record[filetype]");
header("Content-length: $record[filesize]");
header("Content-Disposition: attachment; filename=\"$record[filename]\"");
header("Content-Description: \"sjpds - $record[uid], $record[ip], $record[datetime]\"");
// 생략

lacovnk의 이미지

파일명은 따옴표로 싸주면 좋겠습니다.

간혹 공백 때문에 앞 단어로 다운 받는 경우가.. orz

iolo의 이미지

IE는 Content-Disposition헤더와 무관하게 URL로부터 파일이름을 얻는(마지막 /이후를 파일이름으로 간주하는) 기괴한 동작을 하곤 합니다.
특히 그 파일이 등록된 확장자일 경우 그런것으로 보입니다만... 워낙 기괴한 동작이 많아서 완전히 어떠한 경우인지는 파악할수가 없더군요.

편법이지만, 해당 URL에다가 끝에 /filaname.ext 같이 붙여줘보세요.

그러니까 다운로드해주는 php쪽이 아니고, 요청하는 쪽에

<a href="....download.php?....무슨무슨파라메터있겠죠.../filename.ext">

이런식이시죠. 물론 url인코딩해주셔야겠죠.

----
the smile has left your eyes...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.