doupu1957 2015-01-06 13:22
浏览 25

文本输入到文件上传

I have a form:

<form method="POST" action="">
<textarea id="input_text" name="input_text"></textarea>
<input type="submit" name="decrypt" value="sm">
</form>

now I submit it, php is try to write $_POST['input_text'] to a file then do another action, after complete action, php 'll delete the file created.

<?php
$Path = dirname(__FILE__).'/temp/';
$File_NAME = time().'.txt';
$input_text = stripslashes($_POST['input_text']);
$fp=fopen($Path.$File_NAME,'w');
fwrite($fp,$input_text);
fclose($fp);
//do some curl action with the file, then delete the file
if(file_exists($Path.$File_NAME))
    unlink($Path.$File_NAME);

but if the text too strong, user submit the form, then they abort the page, so the file doesn't delete. I want to direct change the $_POST['input_text'] to type='file', but user also can use it such as a textarea. so php don't need to delete the file because it is a tmp file.

  • 写回答

1条回答 默认 最新

  • doulou9927 2015-01-06 13:32
    关注

    As per your Edit to the question, the possible solution is to first check the $_POST['input_text'] for length before even opening the file. If the text is too long, show an error message.

    I don't think that file is even required in that case.

    OLD ANSWER:

    Fetching the $_POST['input_text], you can :

        $txt = $_POST['input_text'];
        $file = fopen("file.txt", "w+"); //w+ indicates read + write
        fwrite($file,$txt);              //to ride the 'input_txt'
    

    Then perform the actions you want and finally delete the file if required using:

        fclose($file);
        delete("file.txt");
    

    But make sure to grant the PHP page the permissions to Read/Write first.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路