0

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.

5
  • what you exactly want and where is your code? Commented Jan 6, 2015 at 13:26
  • Please can you post your PHP code. From the sounds of it the file is being overwritten, are you using append to file? Commented Jan 6, 2015 at 13:27
  • too long in what sense? Something like a character limit ? Commented Jan 6, 2015 at 13:40
  • @KunalGupta because before delete file, php need to do curl action, if user not wait the curl complete, the file 'll not delete... Commented Jan 6, 2015 at 13:44
  • @HảiLê I updated my answer, is it solved? Commented Jan 6, 2015 at 15:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

oh no, I need to do some curl action with the "file.txt" before delete it,
Then perform the curl actions before deleting the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.