저는 현재 Zend Framework를 사용하는 PHP 프로젝트를 진행하고 있습니다. 컨트롤러에서 문제없이 CSV를 만들고 있지만 사용자가보기의 버튼을 클릭하여 파일을 다운로드 할 수 있기를 원합니다.
내에서 .phtml
:
<a class="btn" href="<?php echo $this->download;?>" download>Export to CSV</a>
$this->download
컨트롤러에서 설정 중입니다.
$view["download"] = $this->_createCSV($bqc_jobs, $startDate, $endDate, $processor_id, $defaultTime);
이 _createCSV
함수는 CSV를 만들어 사이트에서 사용하는 임시 디렉터리에 저장합니다. 그런 다음 파일 경로를 반환합니다.
private function _createCSV($jobs, $start, $end, $user=null, $minutes){
$format = "Ymd_His";
if(!$start && !$user){
$start = date($format, strtoTime("-" . $minutes . " minutes"));
}
if(!$end){
$end = \DateTime::createFromFormat($format, date($format))->format($format);
}
$directory = Config::$tempDir;
$fileName = $directory . "/" . ($user ? $user . "_" : "") . ($start ? $start . "_" : "") . $end . "_report.csv";
$file = fopen($fileName, 'w');
foreach ($jobs as $job){
fputcsv($file, $job);
}
fclose($file);
return $fileName;
}
버튼을 클릭하면 브라우저가 파일 다운로드를 시도하지만 파일을 찾을 수 없기 때문에 오류가 발생합니다. 이것은 브라우저가 임시 폴더에 액세스 할 수 없어야하기 때문에 이치에 맞지만이 문제를 해결하는 방법을 완전히 모르겠습니다.
UNIX 파일 권한으로 인해 폴더를 볼 수없는 경우 유일한 옵션은 다음과 같습니다.
파일 권한이 정상이고 파일을 아파치에서 읽을 수 있다고 확신하면 php의 readfile 함수를 사용 하여 실제로 파일을 브라우저로 다시 전송할 수 있어야합니다.
<?php
$file = '/tmp/monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다