2017-03-03 120 views
1

我想在使用php時將輸出文檔保存在服務器上的文件夾中。該代碼將其保存到我的電腦,但我需要將文件保存在名稱的文件夾中。保存輸出文檔在php

請檢查代碼:

// Save File 
$h2d_file_uri = tempnam('', 'htd'); 
$objWriter = PHPWord_IOFactory::createWriter($phpword_object, 'Word2007'); 
$objWriter->save($h2d_file_uri); 

// Download the file: 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=example.docx'); 
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($h2d_file_uri)); 
ob_clean(); 
flush(); 
$status = readfile($h2d_file_uri); 
unlink($h2d_file_uri); 
exit; 

我試圖file_put_contents( 'test.txt的',的file_get_contents( 'http://web.com'));但它並沒有幫助我。請告訴我如何將文件保存到文件夾中。

回答

0

請使用此代碼將您的輸出保存在名稱的文件夾中,只需提供文件夾名稱和文件名稱 $ fileName = $ _SERVER ['DOCUMENT_ROOT']。'/ download/muraripradip.docx'; $ objWriter = PHPWord_IOFactory :: createWriter($ phpword_object,'Word2007');

0

看起來你正在做很多你不需要的東西。除非你想下載的文件,你可以跳過所有代碼:

// Download the file: - And below that point 

它看起來像你可以只改變現有的代碼一點點:

打開此代碼:

// Save File 
$h2d_file_uri = tempnam('', 'htd'); 
$objWriter = PHPWord_IOFactory::createWriter($phpword_object, 'Word2007'); 
$objWriter->save($h2d_file_uri); 

進入這個:

// Save File 
$h2d_file_uri = $_SERVER['DOCUMENT_ROOT'].'/docs/some_file_name.docx'; 
$objWriter = PHPWord_IOFactory::createWriter($phpword_object, 'Word2007'); 
$objWriter->save($h2d_file_uri); 

如果將被保存到文件夾http://domian.tld/docs/some_file_name.docx。您可以將它保存在公衆不允許訪問的地方。