I found this code. He saves a text file on the server
I want to change it instead to have a textBox and button.
Have the possibility to compose the URL
Example:
www.example.com/index.php?writeText=text
What needs to change in this code to do this?
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
<?php
if(isset($_POST['text_box'])) { //only do file operations when appropriate
$a = $_POST['text_box'];
$myFile = "t.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $a);
fclose($fh);
}
?>
Thanks
You use GET instead of POST.
<?php
if(isset($_GET['writeText'])) { //only do file operations when appropriate
$a = $_GET['writeText'];
$myFile = "t.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $a);
fclose($fh);
}
?>
Related
I am having trouble writing back to a text file selected from a html form with a dropdown menu, the script is able to read but is unable to write back, returns "Unable to Open File".using PHP fopen and fwrite
This is the index.html code:
<html>
<head></head>
<title>Edit Server Side Text Files</title>
<body>
<form action="function.php" method="post">
<select name="list">
<option value="./files/banned-kdf.txt">banned-kdf.txt</option>
<option value="./files/blackList.txt">blackList.txt</option>
</select>
<input type="submit" name="edit" value="Edit" />
</form>
</body>
</html>
And this is the function.php code:
<?php
if(isset($_POST["edit"])) {
$filename = $_POST["list"];
global $filename;
// var_dump($filename);
$myfile = fopen("$filename", "r") or die("Unable to open file!");
$filecontent = fread($myfile,filesize("$filename"));
fclose($myfile);
}
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<textarea name="textareaContent" rows="50" cols="100">
<?
if(isset($filecontent)) { echo $filecontent; }
?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />
</textarea>
<?
//write to text file
if(isset($_POST["update"])) {
// The following two lines are just for troubleshooting and have been commented out.
// echo $_POST["update"];
// var_dump($filename);
$myfile = fopen("$filename", "w") or die("Unable to open file!");
fwrite($myfile, $_POST["textareaContent"]);
fclose($myfile);
}
?>
</form>
<br>
<br>
Return to List Selection
</html>
You're not sending file name in the second form, just code, so, $filename is undefined. Add it in an input hidden:
global $variable; is not useful here, because the variable will not be present on next script execution, when processing the form.
<?php
// Default value for contents:
$filecontent = '';
// Ok, read the file here
if(isset($_POST["edit"])) {
$filename = $_POST["list"];
global $filename; // This will not be useful
$myfile = fopen("$filename", "r") or die("Unable to open file!");
$filecontent = fread($myfile,filesize("$filename"));
fclose($myfile);
}
// If request comes from second form, process it
if(isset($_POST['update'])) {
// Get file name from input hidden
$filename = $_POST["filename"];
$myfile = fopen("$filename", "w") or die("Unable to open file!");
fwrite($myfile, $_POST["textareaContent"]);
fclose($myfile);
// End the script, no need to show the form again
die('File updated successfully.');
}
?>
<html>
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="hidden" name="filename" value ="<?php
// Send filename within form
echo $filename;
?>">
<textarea name="textareaContent" rows="50" cols="100">
<?
// No if needed here, we already have a value
echo $filecontent;
?>
</textarea>
<br>
<br>
<input type="submit" name="update" value="Update" />
</form>
<br>
<br>
Return to List Selection
</html>
Please check the permission of target file and the folder in this the file is supposed to be.
im working on some code to run a .bat file but for some reason the bat the page keeps loading
<?php if (!empty($_POST)){
$myfile = fopen("search.bat", "w") or die("Unable to open file!");
$loc = $_POST["loc"];
$txt = "my bat code";
fwrite($myfile, $txt);
fclose($myfile);
exec("search.bat");
echo "seaching";
}
else{ ?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
loc: <input type="text" name="loc"><br>
<input type="submit">
</form>
<?php }; ?>
I am trying to create a HTML/PHP file in which a user inputs a file name to a text box, clicks submit, then that file is created in the same file folder in the directory
Here is what I have so far, Unfortunatley this my 3rd of fourth attempt, deleting and restarting. I cannot promise it is the best version
<?php
function addExtension($fileName) {
return ($file) + ".txt";
}
if (isset($_POST['prefix'])) {
$result = ($file), (intval($_POST['prefix']));
}
$ourFileName = addExtension;
$ourFileHandle = fopen($fileName, 'w') or die("can't open file");
fclose($ourFileHandle);
?>
<html>
<body>
<?php if (isset($result)) { ?>
<h1> Result: <?php echo $result ?></h1>
<?php } ?>
<form action="" method="post">
<p>File name of file to be created <input type="text" name="prefix" /></p>
<p><input type="submit"/></p>
</body>
</html>
Im not even sure If I'm on the track any more, any help would be great,
Thanks
UPDATE!!!
With the help of the answers below and a few hours I believe I am getting somewhere, But still not 100% there.
Here is what I have.
<html>
<head>
<title>File Creation</title>
<?PHP
$prefix = $_POST['prefix'] ;
$extension = ".css" ;
$ourFileHandle = fopen($prefix, 'w') or die("can't open file");
fclose($ourFileHandle);
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION = "basicForm.php">
<INPUT TYPE = "TEXT" VALUE ="username" NAME="prefix">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">
</FORM>
</body>
</html>
It works as I expect and desire. the issue I'm having is creating the function to combine "$prefix and $extension". I know its something simple, and I have been over it in tutorials previously but which ones I cannot seem to find for the life of me. any more help would be much appreciated!
Just if anyone is curious my intention eventually is to have the script create a file within a folder of the same name, inside a specified folder. The file will be a css file and the contents dynamically changed through the same form.
Thanks Again!
Solved it
Below is the final code/
<html>
<head>
<title>File Creation</title>
<?PHP
if ( isset( $_POST['Submit1'] ) ) {
$prefix = $_POST ['prefix'] ;
$extension = ".css" ;
print "File " .$prefix . $extension . " Created";
$ourFileHandle = fopen($prefix . $extension, 'w') or die("can't open file");
fclose($ourFileHandle);
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION = "fileCreationFormV2.php">
<INPUT TYPE = "TEXT" VALUE ="File Name" NAME="prefix">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Create">
</FORM>
</body>
</html>
I was looking over an old contact form id created and it hit me " . ".
Thanks to everyone for their help!
In your function,
function addExtension($fileName) {
return ($file) + ".txt"; // here you cannot call $file, you can call $fileName
}
I didn't understand your this line
$result = ($file), (intval($_POST['prefix']));
In this line
$ourFileName = addExtension; // you have to call like addExtension('your file name goes here')
Then,
$ourFileHandle = fopen($fileName, 'w') or die("can't open file");
It should be
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
<?php
if(isset($_POST['prefix'])) {
$prefix=$_POST['prefix'];
$filename=$prefix . '.txt';
$ourFileHandle=fopen($fileName, 'w') or die("can't open file");
echo 'done!';
fclose($ourFileHandle);
}
else
{
echo '<html>
<body>
<form action="" method="post">
<p>File name of file to be created <input type="text" name="prefix" /></p>
<p><input type="submit"/></p>
</body>
</html>';
}
?>
I'm very new to PHP and I am trying to use some php code to echo some buttons, and then when the buttons press it opens a file and writes a number in that file. This code doesn't seem to be working and I cant figure out why. Any help is greatly appreciated!
<?php
echo '
<form method="post">
<input type="Submit" name="button1" value="Led1 On" />
</form><br>';
echo '
<form method="post">
<input type="submit" name="button2" value="Led1 OFF" />
</form><br>';
echo '
<form method="post">
<input type="submit" name="button3" value="Led2 On" />
</form><br>';
echo '
<form method="post">
<input type="submit" name="button4" value="Led2 OFF" />
</form><br>';
if (isset($_POST["button1"])){
echo "LED 1 is ON";
$myFile = 'data.txt';
$fh = fopen($myFile, 'w');
fwrite($fh, "1*");
fclose($fh);
}
if (isset($_POST["button2"])){
echo "LED 1 is OFF";
$myFile = 'data.txt';
$fh = fopen($myFile, 'w');
fwrite($fh, "2*");
fclose($fh);
}
if (isset($_POST["button3"])){
echo "LED 2 is ON";
$myFile = 'data.txt';
$fh = fopen($myFile, 'w');
fwrite($fh, "3*");
fclose($fh);
}
if (isset($_POST["button4"])){
echo "LED 2 is OFF";
$myFile = 'data.txt';
$fh = fopen($myFile, 'w');
fwrite($fh, "4*");
fclose($fh);
}
?>
Edit: Seems finding out everyone else got it working lead me to believe it was user error. And it was. Thanks for the help.
Here's a slightly more DRY way to write this code:
$myFile = 'data.txt';
if (is_writable($myFile)) {
$fh = fopen($myFile, 'w');
if (isset($_POST["button1"])){
echo "LED 1 is ON";
$string = "1*";
}
if (isset($_POST["button2"])){
echo "LED 1 is OFF";
$string = "2*";
}
if (isset($_POST["button3"])){
echo "LED 2 is ON";
$string = "3*";
}
if (isset($_POST["button4"])){
echo "LED 2 is OFF";
$string = "4*";
}
fwrite($fh, $string);
fclose($fh);
} else {
echo "The file $myFile is not writable.";
}
You need to run php inside a webserver like Microsoft IIS or Apache HTTP.
Try looking at http://www.php.net/manual/en/getting-started.php
Create data.txt and set permissions 777
<?php
$fn = "content.txt";
if (isset($_POST['content'])){
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
$old_content = file_get_contents($fn);
fputs($fp, $content."\n".$old_content);
fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="40" name="content"></textarea>
<input type="submit" value="Submit">
</form>
So this is my php code. But it deletes the the previously written data. How do I make it so that the new piece of data is added on a new line? Keeping the last of the data.
Thank you!
<?php
$fn = "content.txt";
if (isset($_POST['content'])){
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="40" name="content"></textarea>
<input type="submit" value="Submit">
</form>
Now it doesn't work at all
from fopen manual:
'a' Open for writing only; place the file pointer at the end of the
file. If the file does not exist, attempt to create it.
change
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
to
$fp = fopen($fn,"a") or die ("Error opening file in write mode!");
You can use a or a+ mode instead of w, so you can do:
$fp = fopen($fn,"a") or die ("Error opening file in write mode!");
instead of:
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
From the manual about fopen mode:
'w': Open for writing only; 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.
'a': Open for writing only; place the file pointer at the end of the
file. If the file does not exist, attempt to create it.
'a+': Open for reading and writing; place the file pointer at the end
of the file. If the file does not exist, attempt to create it.
"Now it doesn't work at all"
^--« As per your edit, this will write each entry on a new line:
I changed:
$content = stripslashes($_POST['content']);
to
$content = stripslashes($_POST['content'] . "\n");
Using \n will have all new entries on a new line.
PHP
<?php
$fn = "content.txt";
if (isset($_POST['content'])){
$content = stripslashes($_POST['content'] . "\n");
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="40" name="content"></textarea>
<input type="submit" value="Submit">
</form>
Written to file:
a1
a2
a3
a4
a5
Hello world!!
Testing 123
Written to file one at a time:
a1,a2,a3,a4,a5
Entered in the textarea box as shown and written to file as shown:
Hello world!!
Testing 123
If you want the data to be entered starting from the top, use:
<?php
if (isset($_POST['content'])){
$content = stripslashes($_POST['content'] . "\n");
$file_data = $content;
$file_data .= file_get_contents('content.txt');
file_put_contents('content.txt', $file_data);
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<textarea rows="25" cols="40" name="content"></textarea>
<input type="submit" value="Submit">
</form>
Change your call to fopen to use append mode instead of write:
$fp = fopen($fn,"a") or die ("Error opening file in write mode!");
Source: http://www.php.net/manual/en/function.fopen.php
observation - since you were using file_get_contents already I want to mention that there is a reverse function for writing:
file_put_contents($fn, $content, FILE_APPEND);
this way you don't even need to open up the text file and load it into memory.
Helpful for logging too.