2014-09-09 4 views
-1

이 코드를 사용하여 pdf 파일을 서버에 쓰고 있습니다. 파일을 DATA라는 폴더로 보냅니다. 그리고 나는 mysql에 파일 이름을 저장하고있다.경로가 MySQL에 저장되어있는 PHP를 사용하여 pdf 파일을 다운로드하는 방법은 무엇입니까?

$target = "data/"; $target = $target . basename($_FILES['file']['name']); 

if(isset($_POST['Submit'])) 
{ 
{ 
$path = $_FILES['file']['name']; 
} 

if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
{ 
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 

이제이 pdf 파일을 다운로드 할 수 있도록 도와주십시오.

+0

http://stackoverflow.com/a/17866898/151097 – Rufinus

+1

전에 아무것도 에코 해달라고 확인 정보를 검색하는 방법을 말할 수 없습니다. – RST

+0

파일의 경로를 링크로 표시합니다. –

답변

0

업로드와 MySQL에서 PDF의 이름을 저장 할 수있는 경우에 당신은 DB를 조회하고 PDF의 경로를 구축하고 아래

브라우저

에서 다운로드 강제로 다운로드 헤더를 보내야합니다 몇 가지 코드 당신에게 도움이

HTML

Tno 그래서 파일 PARAM로 다운로드 링크를 전달하고 기본 키는 것 같습니다 행에 대한 동적 링크를 생성

<a href="download.php?file=Tno">Download</a> 

PHP

//Get the file id form $_GET['file']; 
$id = $_GET['file']; 

//Get the row from db 
$sql = "SELECT file FROM tenders WHERE Tno = $id"; 

//Build the path 
$file = "data/" . $mysql_row['file ']; 

header("Content-type:application/pdf"); 
// It will be called downloaded.pdf 
header("Content-Disposition:attachment;filename='downloaded.pdf'"); 
// The PDF source 
readfile($file); 

그 상점 데이터베이스 기능을 보지 않고이 코드

+0

plz 설명 조금 ...이 코드는 새로운 .php 파일에 있어야합니다? 아니면 내가 다운로드 버튼을 준 페이지에서? 새로운 페이지에서 –

+0

은'download.php'처럼 좋을 것입니다. 당신이 우리에게 당신을 더 잘 도울 수있는 PDF의 테이블 열 이름을 보여줄 수 있다면 – Saqueib

+0

INSERT INTO 입찰자 (Tno, Tid, 파일) 값 ('$ Tno', '$ Tid', '$ path'); "file"은 $ path 변수에 수집 된 pdf 파일의 이름을 저장합니다. –

0
// link for download : 
     <a href="download.php"> download </a> 

    // php code inside download.php 
     <?php 
     //download.php 
     $filename="sample.pdf"; // YOUR File name retrive from database 
     $file= "data/".$filename; // YOUR Root path for pdf folder storage 
     $len = filesize($file); // Calculate File Size 
     ob_clean(); 
     header("Pragma: public"); 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Cache-Control: public"); 
     header("Content-Description: File Transfer"); 
     header("Content-Type:application/pdf"); // Send type of file 
     $header="Content-Disposition: attachment; filename=$filename;"; // Send File Name 
     header($header); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: ".$len); // Send File Size 
     @readfile($file); 
     exit; 

     ?> 
관련 문제