2017-03-03 114 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。您可以将它保存在公众不允许访问的地方。