PHP 다운로드 download.php 파일을 통해 파일 다운로드 할 때 아래 사진과 같이 이 파일 형식은 지원되지 않는 것 같습니다. 라는 오류가 발생 할 때 여러 방법을 통해 해 봤지만 안됐습니다.

그러던 중 ob_clean() 코드 한 줄 넣고 성공 했습니다.

 

전체 코드 입니다.

$file = "";//전체 경로
if (file_exists($file)) {
     $filename = basename($file);
     $mime = mime_content_type($file);
     header('Content-Type: ' . $mime);
     header('Content-Disposition: attachment; filename="' . $filename . '"');
     header('Content-Length: ' . filesize($file));
     ob_clean();
     readfile($file);
     exit;
 } else {
     exit('파일이 존재하지 않습니다.');
 }

 

'프로그래밍 > PHP' 카테고리의 다른 글

[PHP] 달력 만들기  (0) 2024.02.16
[PHP] Excel 업로드  (1) 2024.01.13

첫째 주, 마지막 주에 비는 날짜는 저번 달, 다음 달 날짜로 채워야 하는 PHP 달력 코드 입니다.

 

<div>
    <div>
        <div>
            <div>
                <h4>달력</h4>
                <h3>Program</h3>
            </div>
        </div>
        <div class="calendar">
            <dl>
                <dt class="sun">일</dt>
                <dt>월</dt>
                <dt>화</dt>
                <dt>수</dt>
                <dt>목</dt>
                <dt>금</dt>
                <dt class="sat">토</dt>
            </dl>
            <?
            $year = date('Y');
            $month = date("m");
            //$month = date("m",strtotime('2024-06-01')); 테스트 코드

            $firstDayOfMonth = 1;//첫 날
            $lastDayOfMonth = date("t",strtotime("$year-$month"));//마지막 날

            $firstDayOfWeek = date('w',strtotime("$year-$month-01"));//첫날 요일(0~6)
            $lastDayOfWeek = date('w',strtotime("$year-$month-$lastDayOfMonth"));//마지막 날 요일(0~6)

            $lastDiff = 6-$lastDayOfWeek; //다음 달 날짜 표시 해야 될 수
            $prev_last_day=date('t',strtotime("-1 month",strtotime("$year-$month-01")));//지난달 마지막 날

            $prev_count = $prev_last_day - $firstDayOfWeek+1; //첫째 주 저번 달 날짜 표시
            $totalCnt = $firstDayOfWeek+$lastDayOfMonth+$lastDiff;//첫째 주 표시 해야 할 수 + 이번 달 표시 해야 될 수 + 마지막 주 표시 해야 할 수
            $day=1; // 이번 달 날짜 표시
            $last_cnt = 1; //마지막 주 다음 달 날짜 표시
            for($i=1;$i<=$totalCnt;$i++){
                if($i<=$firstDayOfWeek){
                    if($i==1){
                        echo "<dl class=\"body\">";
                    }
                    echo "<dd class=\"block\"><em>{$prev_count}</em></dd>";
                    $prev_count++;
                }else if($i>$lastDayOfMonth+$firstDayOfWeek){
                    echo "<dd class=\"block\"><em>{$last_cnt}</em></dd>";
                    $last_cnt++;
                    if($i==$totalCnt){
                        echo "</dl>";
                    }
                }
                else{
                    if(date("w", strtotime("$year-$month-$day")) == 0){//주 시작(일요일)
                        echo "<dl class=\"body\">";
                    }
                    echo "<dd><em>{$day}</em></dd>";

                    if(date("w", strtotime("$year-$month-$day")) == 6){// 주 종료(토요일)
                        echo "</dl>";
                    }

                    $day++;
                }
            }
            ?>
        </div>
    </div>
</div>

 

 

'프로그래밍 > PHP' 카테고리의 다른 글

[PHP] 파일 다운로드 오류  (0) 2024.04.03
[PHP] Excel 업로드  (1) 2024.01.13

-PHPExcel

PHP 엑셀 업로드 및 다운로드에 필요한 라이브러리

다운로드 주소 : https://github.com/PHPOffice/PHPExcel

 

GitHub - PHPOffice/PHPExcel: ARCHIVED

ARCHIVED. Contribute to PHPOffice/PHPExcel development by creating an account on GitHub.

github.com

 

HTML

<div id="uploadModal">
	<button id="closeBtn" onclick="closeUploadModal()">닫기</button>
    <h2>엑셀 업로드</h2>
    <form id="uploadForm" enctype="multipart/form-data" action="excel_read.php" method="post">
    	<input type="file" name="excelFile" accept=".xls, .xlsx" required>
        <br><br>
        <input type="submit" value="업로드">
    </form>
 </div>
 
 <script type="text/javascript">
    function openUploadModal() {
        document.getElementById('uploadModal').style.display = 'block';
    }

    function closeUploadModal() {
        document.getElementById('uploadModal').style.display = 'none';
    }
 </script>

 

PHP

require_once "../common/PHPExcel-1.8/Classes/PHPExcel.php";
require_once "../common/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php";

if (!$_FILES['excelFile']['name']) {
    echo "<script>alert('엑셀파일을 확인해주세요.')</script>";
    exit;
}

$objPHPExcel = new PHPExcel();
try {
    // 업로드한 PHP 파일을 읽어온다.
    $objPHPExcel = PHPExcel_IOFactory::load($_FILES['excelFile']['tmp_name']);
    $objPHPExcel -> setActiveSheetIndex(0);//첫번째 시트를 선택
    $sheet = $objPHPExcel->getActiveSheet();
    $rows = $sheet->getRowIterator();
    foreach ($rows as $row) { // 모든 행에 대해서
        $cellIterator = $row->getCellIterator();
        $cellIterator->setIterateOnlyExistingCells(false);
    }
    $maxRow = $sheet->getHighestRow();
    for ($i = 0 ; $i <= $maxRow ; $i++) {
        $A = $sheet->getCell('A' . $i)->getValue(); // A열
        $B = $sheet->getCell('B' . $i)->getValue(); // B열
        $C = $sheet->getCell('C' . $i)->getValue(); // C열
        $D = $sheet->getCell('D' . $i)->getValue(); // D열
        $F = $sheet->getCell('E' . $i)->getValue(); // E열
        $G = $sheet->getCell('F' . $i)->getValue(); // F열
        $H = $sheet->getCell('H' . $i)->getValue(); // H열
        $I = $sheet->getCell('I' . $i)->getValue(); // I열
    }
}catch(exception $e) {
    echo $e;
}

 

만약 시트가 여러개인 경우 아래 코드 참고.

$sheetsCount = $objPHPExcel -> getSheetCount();
// 시트Sheet별로 읽기
for($i = 0; $i < $sheetsCount; $i++) {
    $objPHPExcel -> setActiveSheetIndex($i);
    $sheet = $objPHPExcel -> getActiveSheet();
    $highestRow = $sheet -> getHighestRow();   		// 마지막 행
    $highestColumn = $sheet -> getHighestColumn();	// 마지막 컬럼

    // 한줄읽기
    for($row = $start_row; $row <= $highestRow+$start_row-1; $row++) {
        // $rowData가 한줄의 데이터를 셀별로 배열처리 된다.
        $rowData = $sheet -> rangeToArray("A" . $row . ":" . $highestColumn . $row, NULL, TRUE, FALSE);

        // $rowData에 들어가는 값은 계속 초기화 되기때문에 값을 담을 새로운 배열을 선안하고 담는다.
        $allData[$row] = $rowData[0];
    }
}

 

'프로그래밍 > PHP' 카테고리의 다른 글

[PHP] 파일 다운로드 오류  (0) 2024.04.03
[PHP] 달력 만들기  (0) 2024.02.16

+ Recent posts