0

wordpress サイトで textarea コンテンツを .txt ファイルとしてダウンロードする必要があるプロジェクトに取り組んでいます。

以前の質問を検索した後、正解としてマークされた以下のコードを取得しました。

<html>
<body>
<form action="#" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit">Download Text</input>
</form>

<?PHP

if(isset($_POST['submit']))
 {   

$text = $_POST['text'];
print ($text);

$filename = 'test.txt';
$string = $text;

$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);

header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');

 }

?>

</body>
</html>

しかし、私にとっては.txtファイルを作成していません。問題を特定するのを手伝ってください

4

3 に答える 3

3

コードを少し変更しました。

最初にあなたのphpコードを入れてください

 <?php

    if(isset($_POST['submit']))
     {   

    $text = $_POST['text'];
    print ($text);

    $filename = 'test.txt';
    $string = $text;

    $fp = fopen($filename, "w");
    fwrite($fp, $string);
    fclose($fp);

    header('Content-disposition: attachment; filename=test.txt');
    header('Content-type: application/txt');
    readfile('test.txt');
    die; //modified code 
     }
    ?>

その後、あなたのhtmlコードを入れてください

<html>
<body>
<form action="#" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

送信ボタンに name 属性を追加します。

于 2013-08-14T07:09:36.407 に答える
1

コードは実際にサーバー上にファイルを作成し、後で削除されません。コードが失敗した場合は、サーバーに書き込む権限がなかった可能性があります。プロセスでファイルが生成されないように、コードを次のようにトリミングできます。

<?php
if(isset($_POST['text']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text'];
   exit; //stop writing
 }
?>
<html>
<body>
<form action="" method="post">
<textarea name="text" rows="20" cols="100"></textarea>
<input type="submit" value="submit" name="submit">Download Text</input>
</form>

</body>
</html>

まず、画面に何かを印刷する前にファイルを出力する必要があります。次に、name="submit"元のコードに追加するのを忘れていました。ここでは使用しませんでしたが、アイデアを得るために含めました。


WordPress コンテキスト:functions.phpまたはプラグイン ファイルにこれを追加します。

function feed_text_download(){
if(isset($_POST['text_to_download']))
 {
   header('Content-disposition: attachment; filename=test.txt');
   header('Content-type: application/txt');
   echo $_POST['text_to_download'];
   exit; //stop writing
 }
}
add_action('after_setup_theme', 'feed_text_download');

フォームをテンプレート ファイルの 1 つに追加するか、投稿の HTML エディターに追加します。

<form action="" method="post">
  <textarea name="text_to_download" rows="20" cols="100"></textarea>
  <input type="submit" value="submit" name="submit">Download Text</input>
</form>

それは良いはずです:)


編集:ファイル名を追加

HTML:

<form action="" method="post">
  <label>Filename:<input type="text" name="filename" /></label>
  <label>Text:<textarea cols="100" name="text_to_download" rows="20"></textarea></label>
  <input type="submit" name="submit" value="Download Text" />
</form>

PHP:

function feed_text_download() {
  if ( isset( $_POST['text_to_download'] ) && isset( $_POST['filename'] ) ) {

    $filename = sanitize_file_name( $_POST['filename'] );

    header( 'Content-disposition: attachment; filename=' . $filename );
    header( 'Content-type: application/txt' );
    echo $_POST['text_to_download'];
    exit; //stop writing
  }
}

add_action( 'after_setup_theme', 'feed_text_download' );
于 2013-08-14T08:14:41.887 に答える
0

次のコードを試してみてください。

<?php  ob_start();?>
<html>
<body>
<?php

     if(isset($_POST['tarea'])){
     $filename = 'test.txt';
     $data = $_POST['tarea']; 
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$filename");
     header("Content-Type: application/octet-stream; "); 
     header("Content-Transfer-Encoding: binary");
     echo $data;
     exit;
}
?>
<form action="" method="post">
<textarea name="tarea"></textarea>
<input type="submit">     
</form>
</body>
</html>
于 2013-08-14T06:54:51.437 に答える