티스토리 뷰

Language/PHP

파일 다운로드

MiniNeko 2015. 11. 29. 04:03

$F = "좌상공파수단신청서_개인별_이름.hwp";
$Fullfile = $_SERVER[DOCUMENT_ROOT]."/mspark/". $F;
//$F = iconv("UTF-8", "EUC-KR", $F);
//$F = iconv("EUC-KR", "UTF-8", $F);

// 파일 체크
if (!is_file($Fullfile)) 
{
echo"
< Script Language='JavaScript'>
   alert('해당 파일이 존재하지않습니다.!');
   history.back();
< /Script>
";
}

header("Cache-Control: private, must-revalidate"); 
header("Content-type: application/octet-stream" ); 
header("Content-Length: ".(string)(filesize($Fullfile)));   
header("Content-Disposition: attachment; filename=" . $F);
header("Pragma: no-cache");
header("Expires: 0");   
$fp = fopen($Fullfile, "r");
if (!fpassthru($fp)) fclose($fp);
exit;


===============================================================================

 $dir="/home/users/www/download/"; 
 $filename = "MyFilename.exe";
   
 header("Content-Type: application/octet-stream");
 header("Content-Disposition: attachment;; filename=$filename");
 header("Content-Transfer-Encoding: binary"); 
 header("Content-Length: ".(string)(filesize($dir.$filename))); 
 header("Cache-Control: cache, must-revalidate"); 
 header("Pragma: no-cache"); 
 header("Expires: 0");
 $fp = fopen($dir.$filename, "rb"); //rb 읽기전용 바이러니 타입
 if(!fpassthru($fp)) {
     fclose($fp);                        
 }
 
상기 코드를 사용하면 아래 그림과 같이 다운로드 링크를 클릭시 바로 다운로드가 됩니다.
 http://cfs5.tistory.com/upload_control/download.blog?fhandle=YmxvZzQzNTQ2QGZzNS50aXN0b3J5LmNvbTovYXR0YWNoLzAvMC5KUEc%3D

 
 $dir="/home/users/www/download/";   웹의 루트가 아닌 실질적인 디렉터리를 지정합니다.

===============================================================================

if($mode == "FILEUP") {
   // FILE UPLODE - filePath 경로, $_FILES["파일"] 수정해야함
   $filePath = $_SERVER['DOCUMENT_ROOT']."/upfile/TRAINING/";
      $myFile = $_FILES["txtFile"]["name"]; // String name into variable
   $newFileName = date("Ymdhis").(iconv("utf-8","euc-kr",$myFile)); // New string is concatenated

   if(file_exists($filePath.$newFileName)) {
      echo $newFileName." already exists. ";
   } else {
      $saveFileName = iconv("euc-kr","utf-8",$newFileName);
      copy($_FILES["txtFile"]["tmp_name"],$filePath.$newFileName);
   }

   // 등록된 파일이 있으면 삭제 시작
   $querySelect="SELECT * FROM training_file WHERE ptype='".$pmode."' AND sid='".$txtSid."'";
   $resultSelect = mysql_query($querySelect);
   $rowNumSelect = mysql_affected_rows();
   if($rowNumSelect > 0) {
      $rowDataSelect = mysql_fetch_array($resultSelect);
      $deleteFileTemp = $filePath.$rowDataSelect['fname'];
      //chmod($deleteFileTemp, 0777);
      //unlink($deleteFileTemp); // 파일 업로드 권한만 있어서 삭제가 안됨.

      $queryInsert="UPDATE training_file SET
         fname='".$saveFileName."',
         regDate=now()
            WHERE sid='".$txtSid."' AND ptype='".$pmode."' LIMIT 1
         ";
      $resultInsert=mysql_query($queryInsert);
   // 등록된 파일이 있으면 삭제 끝
   } else {
      $queryInsert="INSERT INTO training_file SET
         sid='".$txtSid."',
         ptype='".$pmode."',
         fname='".$saveFileName."',
         regDate=now()
      ";
      $resultInsert=mysql_query($queryInsert);
   }
}

if($mode == "FILEUP") {
   if($pmode == "AG") { $pmodeStr = "현장실습 협약서"; }
   if($pmode == "BS") { $pmodeStr = "업체 사업자등록증"; }
   if($resultInsert) {
      echo "<script>alert('".$pmodeStr." 정상 등록 되었습니다.');</script>";
      echo "<script>location.href='/html/30/05_job.php';</script>";
      //echo "<script>location.href='/';</script>";
   } else {
      echo "<script>alert('".$pmodeStr." 등록에 실패 하였습니다.');</script>";
      echo "<script>history.back();</script>";
   }
}

'Language > PHP' 카테고리의 다른 글

mysql_query 의 두가지 문법  (0) 2015.11.29
세션, 쿠키 정리  (0) 2015.11.29
문자열을 구분자로 나누기  (0) 2015.11.29
strstr() 문자열 : 특정문자 기준 : 나누기  (0) 2015.11.29
배열 값 POST 로 넘기기  (0) 2015.11.29
Total
Today
Yesterday
최근에 올라온 글
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31