私は現在cakephpで作業しており、単語文書を生成しています。私の問題は、生成されたドキュメントをダウンロードではなく、Web ルートに配置する方法です。
2 に答える
webroot にファイルを追加したいのですが、パブリック ユーザーはダウンロードできません
。
いくつかの方法があり
ます
。Cakephp の Dispatcher フィルター: http://book.cakephp.org/2.0/en/development/dispatch-filters.html
および ....
アクションを使用してドキュメントを生成し、ブラウザに出力していると思います。
出力バッファリングを使用して出力を「キャッチ」してからファイルに書き込むか、ドキュメント データを文字列に書き込んでその文字列をサーバー上のファイルに書き込む必要があります。
編集: PHPWord には SAVE メソッドがあります。アクションでは、ドキュメントを特定の場所に保存できますが、別のもの、つまり成功通知を出力できます。このように、アクションはファイルのみを生成します。
public function generateWordDocument(){
//... your word file creation...
$wordDocumentLocation = TMP . 'word_files/';
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($wordDocumentLocation . 'helloWorld.docx');
$this->Session->setFlash('Document generated!');
$this->redirect(array('action'=>'index')); //or wherever you want
}
そのファイルを保護したい場合は、ファイルを「安全な」フォルダー (「app/webroot」フォルダー外のフォルダー、または .htaccess deny all 命令で保護されたフォルダーのいずれか) に保存し、別のフォルダーを使用することができます。 「getWordDocument」などのアクション:
function getWordDocument($documentName){
$wordDocumentLocation = TMP . 'word_files/';
if (file_exists($wordDocumentLocation . $documentName)) { //this is not really the safest way of doing it
$fp = fopen($wordDocumentLocation . $documentName, 'rb');
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Length: " . filesize($wordDocumentLocation . $documentName));
fpassthru($fp);
exit();
}
}
このコードは単に「概念を把握する」ためのものであり、決して安全でも最適でもないことに注意してください。