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 }; ?>
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.
I'm trying to create a program that uploads txt/docx file on my folder and convert it to pdf, but whenever i open the pdf file, it shows error: not a pdf or corrupted file, here is my code
//html
<!DOCTYPE html>
<html>
<body>
<form action="trylngnangtry.php" method="post" enctype="multipart/form-data">
Select Text file to Convert:
<input type="file" name="fileToUpload">
<input type="submit" value="Convert" name="submit">
</form>
</body>
</html>
//php
<?php
$target_dir = "C:/xampp/htdocs/mine/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$myfile = fopen("ConvFile.pdf", "w") or die("Unable to open file!");
$basa = file_get_contents(basename( $_FILES["fileToUpload"]["name"]));
fwrite($myfile, $basa);
fclose($myfile);
echo file_get_contents("ConvFile.PDF");
} else {
$myfile = fopen("ConvFile.pdf", "w") or die("Unable to open file!");
$basa = file_get_contents(basename( $_FILES["fileToUpload"]["name"]));
fwrite($myfile, $basa);
fclose($myfile);
echo file_get_contents("ConvFile.PDF");
}
?>
My program uses PHP to open a list of configuration settings from a txt file called "configurationSettings.txt" and puts the data from it onto a form.
What I'm trying to figure out is how to enable my program to update the data on the original txt file if the user changes anything through the form.
Here is an example of the txt file data:
Channel 7
4.0000
6.0000
Here is my code that reads the data and fills my form:
<?php
$configFile = fopen("configurationSettings.txt", "r");
$title1 = fgets($configFile);
$gain1 = fgets($configFile);
$offset1 = fgets($configFile);
fclose($configFile);
?>
<form action="program.php" method="post">
Channel 8 Title:<br>
<input type="text" name="channel0Title" value="<?php echo $title1 ?>">
<br>
Gain:<br>
<input type="text" name="channel0Gain" value="<?php echo $gain1 ?>">
<br>
Offset:<br>
<input type="text" name="Channel0Offset" value= "<?php echo $offset1 ?>">
<br>
<input type="submit" id ="submitButton" value="Submit">
</div>
</form>
And heres a picture of what it looks like:
What do I do to update the original txt file by pressing the submit button?
Tested, works 100%. You don't have to create .txt. Gets created automatically if not present.
index.html
<form action="program.php" method="post">
Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
Gain:<br><input type="text" name="channel0Gain" value="4.000"><br>
Offset:<br><input type="text" name="channel0Offset" value= "6.000"><br>
<input type="submit" id ="submitButton" value="Submit">
</form>
program.php
<?php
$title = $_POST["channel0Title"]; //You have to get the form data
$gain = $_POST["channel0Gain"];
$offset = $_POST["channel0Offset"];
$file = fopen('configurationSettings.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $title. PHP_EOL .$gain. PHP_EOL .$offset;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die("There was an error writing this file");
}
else {
echo "$ret bytes written to file";
}}
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);
}
?>
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