Converting text to file and save it to in a folder - php

I have been trying to save input contact as a file in a folder but i don't know how to go about this using php.
<php
if(isset($_POST['s'])){
$neexe = $_POST['nameextention'];
$filecont = $_POST['fileBody'];
$folderpath = 'Files';
// I wan to save this as page.php with the body content inside so users can download it
}
?>
<form method="post">
<input type="text" name="nameextention" value="page.php"/>
<textarea name="fileBody"><?php if(isset($_HELP['please'])){echo 'i really need to get this done!!';}?></textarea>
<input type="submit" name="s" value="Save">
</form>

Easy. use fopen built-in function as below:
$myfile = fopen($neexe, "w") or die("Failed to open file!");
fwrite($myfile, $filecont);
fclose($myfile);

Related

PHP: Editing a file using a dynamic filename

I have a PHP script that I am trying to edit some text files that have a dynamic file name. I think I am about 90% there, but am having an issue where the resulting output filename is incorrect.
The files in question I am trying to edit are for VoIP phones nad have a ormat of MAC.cfg, where MAC is the MAC address of the phone in question.
I have an HTML page that is just a very simple form where the MAC address is entered, and the form calls a PHP script that using that MAC address and brings up the contents of the appropriate MAC.cfg file. When I edit what I want to edit, however, the resulting filename becomes MAC.cfg.cfg (i.e., it adds an extra ".cfg" to it), and I do not know why.
The code for the page where the MAC address of the phone gets entered is:
<form method="post" action="edit-imaccfg.php">
<input type="text" name="macaddress" value="">
<input type="submit">
</form>
and the contents of 'edit-imaccfg.php' is this:
<?php
// set file to read
$filename = $_POST['macaddress'].=".cfg";
$newdata = $_POST['newd'];
if ($newdata != '') {
// open file
$fw = fopen($filename, 'w') or die('Could not open file!');
// write to file
// added stripslashes to $newdata
$fb = fwrite($fw,stripslashes($newdata)) or die('Could not write to file');
// close file
fclose($fw);
}
// open file
$fh = fopen($filename, "r") or die("Could not open file!");
// read file contents
$data = fread($fh, filesize($filename)) or die("Could not read file!");
// close file
fclose($fh);
// print file contents
?>
<html>
<body>
<form action="<?= $_SERVER[php_self] ?>" method="POST" >
<textarea name="newd" cols="100%" rows="100"> <?= $data ?> </textarea>
<input type="hidden" name="macaddress" value="<?= $_POST['macaddress'] ?>" />
<input type="submit" value="Change">
</form>
</body>
</html>
I don't know what, in the above code, is adding the extra '.cfg' to the outputted filename, rather than overwriting the intended MAC.CFG file.
Your insight is greatly appreciated Thanks! :-)

How can I get the output for form without the page changing

I am trying to get information from textarea to be converted to a .py file.
Here's my file https://jsfiddle.net/girlwhocancode/68mb49gq/
<form action=action.php method="post">
<center>
<textarea placeholder="Code you want to execute in python..."></textarea>
</center><br/>
<center><input type="submit" class="button_example"></center>
</form>
and this is my php file:
<?php
$path = #PATH;
if (isset($_POST['field1'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
For some reason my php isn't working and I have no idea how to keep the same webpage after submitting
Try this:
<form action="action.php" method="post">
<center>
<textarea name="script" placeholder="Code you want to execute in python..."></textarea> //added name
</center><br/>
<center><input name="submit" type="submit" class="button_example"></center>
</form> // added name submit
in php
<?php
$path = #PATH;
if (isset($_POST['submit'])) {
$fh = fopen($path,"a+");
$string = $_POST['script'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Remember that in php $_POST/$_GET keys are always your form field
names

How to pass variable one file to another using f write function

Hi how to pass variables from one file to another file with f write
i have a file name a.php
code is
<?php
if(isset($_POST['psub'])){
$myfile = fopen("an.php", "w") or die("Unable to open file!");
$pst = $_POST['pst'];
fwrite($myfile,$pst);
fclose($myfile);
}
?>
<form action="" method="post">
<input type="text" name="pst">
<input type="submit" name="psub" value="submit">
</form>
i need in an.php
$abc = "GET VALUE FROM $pst"
It looks like you approach this the wrong way.
Add an.php as an action in your form in a.php and, if you need to, redirect back to it after processing needed data.
Your a.php:
<form action="an.php" method="post">
<input type="text" name="pst">
<input type="submit" name="psub" value="submit">
</form>
and your an.php:
if( isset($_POST['psub']) && isset($_POST['pst']) ){
// do what you need with it
$abc = "GET VALUE FROM $_POST['pst']";
}
// some other work that you need
header("Location : a.php");

save post from file into msword

i want to save a post from an html form into a msword file
<form method="post" action="process.php">
<text name="comment"></text>
<input type="submit" name="send" value="submit"/>
</form>
process.php
<?php
if(isset($_POST['submit']))
{
$body=$_POST['comment'];
$newfile = fopen("essay/last.docx", "w") ;
$content = "$body";
fwrite($newfile, $content);
fclose($newfile);
}
?>
the created file is not a valid msword file. any help is appreciated

PHP write data from HTML to text file

I need to write some data from HTML form to data.txt file (the txt file have 777 permissions)
The HTML that I use is index.html:
<form action="index.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
And the PHP file is index.php:
<?php
$txt = "data.txt";
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$fh = fopen($txt, 'a');
$txt=$_POST['field1'].' - '.$_POST['field2'];
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>
But when I use the form to save the data, it show me a message box to download the index.php
what I am doing wrong?

Categories