我想要做的是使用用戶輸入更改文件並將更新後的文件上載回用戶。在服務器上更改文件後將文件上傳到用戶
現在我有一些Java腳本,從輸入收集數據:
$('#collectButton').click(function() {
var inputs = $('#someDiv input').get();
因爲我收集我需要將其發送給我的PHP代碼數據,它是由阿賈克斯郵政和JSON(陣列轉移)完成。
$.ajax({
type: "POST",
url: "Some.php",
data: {postedData:JSON.stringify(inputs)},
success: function()
{
alert('done!');
}
});
Firebug的控制檯確認數據傳輸和來這裏的問題:
對此我看到了改變的文件(TXT簡單),這應該是上傳回來,但事實並非如此。
PHP,我用的是:
if (isset ($_POST['postedData'])){
$changes = (json_decode($_POST['postedData']));
....
some changes done by for loop
....
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($nwFile));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($nwFile));
ob_clean();
flush();
readfile($nwFile);
// deletes file right after
fclose($nwFile);
unlink($nwFile);
如何去做正確的?
爲什麼PHP的「header」部分被忽略?
PS
獨立的PHP文件工作格柵。
問題只出現在「調用PHP」
您是否確認$ nwFile實際上是在服務器上創建的?另外,你爲什麼在頭文件之後立即調用ob_clean()? – Yaniro 2011-12-30 20:31:36
Yaniro - 是$ nwFile創建的 ob_clean() - 是一個問題嗎?它正在清理輸出緩衝區。我不需要緩衝區,文件中的所有數據。 – owner 2011-12-30 21:25:35
嘗試在標題前使用ob_clean(),但是你真的需要它嗎? – Yaniro 2011-12-30 21:36:05