Text input to file upload - php

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.

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.

Related

PHP- saving input to separate files with date name

I did a code in PHP where input is saved to the file 'news'. Now I would like to change it that it will save every input to separate file in special folder(news) which name starts with date.
I wrote something like this but it isn't working (the file isn't being created)
<?php
include "0begin.php";
$title=$_POST["title"];
isset($title) or $title=$_GET["title"];
$msg=$_POST["msg"];
?>
<h1>News</h1>
<form method=post>
Title<br><input type=text input name="title" value=<?=$title?> ><br>
Message<br>
<textarea input name="msg" cols=40 rows=5> </textarea><br>
<input type="submit">
<br><br>
</form>
<?php
$msg = $_POST['msg'];
$dateposted = date("YmdHis");
$fp = fopen("$dateposted.txt", "w");
fwrite($fp,$title, $msg).' ';
fclose($fp);
?>
<?php
include "0end.php";
I think your Problem lies in the fopen and fwrite you are using.
fopen ("$dateposted.txt","w") will create you a file with the name $dateposted.txt. I think you rather want fopen($dateposted."txt","w").
Since the file isn't created, I would check the folder permissions of the destination folder and modify them if the user that runs the script, for example an apache on linux isn't allowed to write there. Or set the complete Destinationpath to be sure the file is stored in the correct destionation.
Another thing regarding the fwrite. From what I know, fwrite accepts 2 Parameters. File and String. The third possible parameter is the maximum length of added bytes. In your case I would call fwrite once for the title and once for the message. Furthermore the string concatenation on the fwrite looks wrong to me, since I assume the concated String should be in the file you would need to write for example fwrite($fp, $title.' ');

PHP Upload File AND Write TXT File

This SHD work ... no idea why.
When user submits form, uploads the file to ../images/ and writes that file name to a text file. It does not upload the file and it blanks out the txt file which I assume means the upload fails.
$img_name = $_FILES['avatar']['name'];
$upload_file = move_uploaded_file($_FILES['avatar']['tmp_name'], "../images/" . $img_name);
$log_avatar = fopen('../archive/avatar.txt', 'w+');
fwrite($log_avatar, $img_name);
fclose($log_avatar);
header("location:user.php");
to write the name of the uploaded file to text file use "a+" instead of "w+"
the difference between them is
"w+" opens the file for reading and writing and place the file pointer at the beginning of the file
"a+" opens the file for reading and writing and place the file pointer at the end of the file
$log_avatar = fopen('../archive/avatar.txt', 'a+');
also you can add line break using PHP_EOL
fwrite($log_avatar, $img_name.PHP_EOL);
that is what makes the text file get empty
about upload files there is nothing wrong in your code.
but i think you are have error in your html form because filename is blank
make sure you set the form enctype Attribute to "multipart/form-data"
<form method="POST" enctype="multipart/form-data" action="up.php">
and using the correct input name in your upload script

edit my centos 6 network config file using PHP code but after network restart i get an error

I am using a simple file operation on the PHP in order to edit the config file for network interface on CentOS 6.7(/etc/sysconfig/network-scripts/ifcfg-eth0
), after change in any value and save into the config file and try to restart the network interface i get this error:
does not seem to be present, delaying initialization.
[FAILED]
my PHP code is this:
<?php
// configuration
$file = '/etc/sysconfig/network-scripts/ifcfg-eth0';
// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
file_put_contents($file, $_POST['text']);
// redirect to form again
header('Location: network.php');
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea style="width:50%; height:50%;" name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>
i need manually called the network script by command setup and do a modification in the device setting and save then i will be able to restart the network interface. appreciate if anyone help me why this issue happen while if i open the config file and edit it manually it wouldn't cause this issue.
It's most likely that the user name the server runs as (by default apache on most Red Hat-based distributions) doesn't have permission to write to/etc/sysconfig/network-scripts/ifcfg-eth0.
You should check the return values of:
file_put_contents($file, $_POST['text']);
From http://php.net/manual/en/function.file-put-contents.php
This function returns the number of bytes that were written to the file, or FALSE on failure.
I presume this script will only be run by your or trusted colleagues given that there's no validation of the user input. Indentation would also make the PHP code more readable.

Clear fwrite log files in php

Is there a way I can have 5 logs files cleared on a frontend, without emptying the file data manually or deleting them?
I want to add a button or such on a page that will empty them if possible.
You can not clear a file on your server on frontend. You can push a button on frontend and call a backend script.
w+ : Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
$handle = fopen ("/path/to/file.txt", "w+");
fclose($handle);
clearlogs.php
<?php
$logfiles = array(
"1-LOG.txt",
"2-LOG.txt",
"3-LOG.txt",
"4-LOG.txt",
"5-LOG.txt",
);
foreach ($logfiles as $logfile){
$clearlogs = #fopen("$logfile","r+");
#ftruncate($clearlogs, 0);
}
header("Location:logs.php");
?>
logs.php
<form action="clearlogs.php" method="POST">
LOGS <input type="submit" value="Clear Logs" />
</form>

Form empty itself without doing anything

Earlier today I asked a question about what I was doing wrong, I got this working but now I'm running into another problem with this script.
Previously
The code gave a warning(), that's fixed now. You can read the post Here
What's the problem?
The code automatically empty itself. When you refresh the page where the script is the text file is empty. I have no idea why...
This is the code
<?php
$fn = "file.txt";
$file = fopen($fn, "w+");
$size = filesize($fn);
if($_POST['addition']) fwrite($file, $_POST['addition']);
fclose($file);
?>
<form action="<?=$PHP_SELF?>" method="post">
<input type="text" name="addition" value="<?php echo file_get_contents('file.txt');?>"/>
<input type="submit"/>
</form>
I use this script to display a youtube video on my website, so I got to update it often.
You can find a working example of the script with this link: http://beta.martijnmelchers.nl/private/Test/test.php
What have i tried?
I didn't try many because I couldn't find a solution for this on the internet and also not in the code.
Please help me again! Thanks in advance!
According to the manual with the w+ option:
Open for reading and writing; place the file pointer at the beginning
of the file and truncate the file to zero length. If the file does not
exist, attempt to create it.
It looks like you want to replace all contents when a post is made, so the easiest solution is to put all file-handling calls in the POST condition:
// To avoid warnings, this is better.
// You can add your original condition after it if you need it.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$file = fopen($fn, "w+");
// not sure why you need this...
// $size = filesize($fn);
fwrite($file, $_POST['addition']);
fclose($file);
}

Categories