사용자가 다음 양식을 제출했지만 최종 장애물에 문제가있는 경우 .txt 파일을 다시 작성하고 다운로드하려고합니다. 제출 버튼 내가 다음 (간체) 코드를 포함하는 jQuery를 기능을 실행하고 누르면 PHP를 사용하여 파일 생성 및 다운로드
<form id="saveFile" action="index.php?download" method="POST" onsubmit="return false;">
<input type="submit" name="backupFile" id="backupFile" value="Save a file" />
<input type="hidden" name="textFile" id="textFile" />
</form>
- 난 내가 실제로 저장하고있는 무슨의 코드를 촬영하고 간단한 문자열로 대체했다. 그러면 '숨겨진'입력 유형이 설정되고 양식이 제출됩니다. 양식이 제출되면
$("#textFile").val('This is the text I will be saving');
document.forms['saveJSON'].submit();
는 다음과 같은 PHP 코드가 실행됩니다 :
<?php
if (isset($_GET['download'])) {
$textData = $_POST["textFile"];
$newTextData = str_replace("\\","",$textData);
$myFile = "php/data.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $newTextData);
fclose($fh);
header("Cache-Control: public");
header("Content-Description: File Download");
header("Content-Disposition: attachment; filename='".basename($myFile)."'");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile($myFile);
}
?>
하여 최대 FCLOSE ($ FH)에; 실제로 코드는 잘 작동합니다. .txt 파일은 필요한 새 내용으로 업데이트되고 파일이 다운로드됩니다. 그러나 다운로드 한 파일을 열면 웹 페이지 (index.php)의 많은 내용과 함께 .txt 파일 텍스트가 포함됩니다.
는 예를 들어, 파일이
처럼 보일 수 있습니다
"이것은이 내 콘텐츠의 나머지 얹는 내 웹 사이트의 H1입니다 내가
을 절약 할 텍스트입니다"
누가이 문제의 원인인지 알고 계십니까?
이렇게 출력하고 있습니다. 그렇게하지 않으면 그 일은 일어나지 않을 것입니다. 다운로드 제공만을 담당하는 스크립트로 리디렉션 (또는 링크)하면 쉽게 돌아갈 수 있습니다. – hakre