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! :-)
Related
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
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);
I want to use fwrite to save data into a .txt file. The action method seems to be working, as it can show HTML tags when being transfered when pressing submit, but i wont run the PHP.
<HTML lang="da">
<style>
</style>
<header>
<title>Tilføj</title>
<meta charset="ISO-8859-1">
</header>
<body>
<form method="post" action="eksamen_save_data.php" enctype='multipart/form-data'>
<fieldset>
<legend>Filmoplysninger</legend>
<div><label>Titel: <input type="text" name="titel" id="titel" required="required" size="60" maxlength="100"></label></div>
<div><label>Hovedskuespiller: <input type="text" name="hovedskuespiller" id="hovedskuespiller" required="required" size="30" maxlength="100"></label></div>
<div><label>Genre: <input type="text" name="genre" id="genre" required="required" size="60" maxlength="100"></label></div>
<div><label>Format: <input type="text" name="format" id="format" required="required" size="60" maxlength="100"></label></div>
<div><label>Billede: <input type="file" name="billede" id="billede" required="required"></label></div>
</fieldset>
<div><input type="submit" id="ok" value="OK"></div>
</form>
</body>
This sends it to the "eksamen_save_data.php" that looks like this:
<?php
$Titel = $_POST["titel"];
$Hovedskuespiller = $_POST["hovedskuespiller"];
$Genre = $_POST["genre"];
$Format = $_POST["format"];
//$Billede = $_FILES["billede"]["navn"];
//if($_FILES){
// move_uploaded_file($_FILES["billed"]["navn"], $_FILES["billed"]["navn"]);
//}
$user_data = "$Titel, $Hovedskuespiller, $Genre, $Format, $Billede \r\n";
$fh = fopen("data.txt", "a") or die("Fejl i åbning af fil!");
fwrite($fh, $user_data) or die ("Fejl i skrivning til fil!");
fclose($fh);
?>
If i write some HTML in the "eksamen_save_data.php" i can show this, but it wont run the PHP. I'm using XAMPP.
The problem is that it wont save to the "data.txt" file as i tell the PHP to do.
Another question; is there also a way, I can make the PHP run in the same file as where I have my fieldset?
LAST EDIT:
Most of the time it's the little mistakes that proves to be the biggest problem. For me i personally forgot to use: localhost/eksamen_tilføj.php in the browser.
So it was me making a mistake in XAMPP.
Use file_put_contents
file_put_contents("data.txt", $user_data, FILE_APPEND);
It does all the jobs like file open, write and close. Advantage is if the file does not exist then it will create.
Find full working code
<?php
$Titel = $_POST["titel"];
$Hovedskuespiller = $_POST["hovedskuespiller"];
$Genre = $_POST["genre"];
$Format = $_POST["format"];
$Billede = $_FILES["billede"]["name"];
// Example of accessing data for a newly uploaded file
$fileName = $_FILES["billede"]["name"];
$fileTmpLoc = $_FILES["billede"]["tmp_name"];
// Path and file name
$pathAndName = "upload/".$fileName;
// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
// Evaluate the value returned from the function if needed
if ($moveResult == true) {
echo "File has been moved from " . $fileTmpLoc . " to" . $pathAndName;
} else {
echo "ERROR: File not moved correctly";
}
$user_data = "$Titel, $Hovedskuespiller, $Genre, $Format, $Billede \r\n";
file_put_contents("data.txt", $user_data, FILE_APPEND);
?>
you'll have to remove the third parameter $test (because it specifies the length of the content to be written). But $test is not defined in your PHP file, so it won't write anything..
So change this
fwrite($fh, $user_data, $test) or die ("Fejl i skrivning til fil!");
into this
fwrite($fh, $user_data) or die ("Fejl i skrivning til fil!");
and have a look at this :)
As for your second question: sure you can merge your form and submit scripts:
<?php
if(count($_POST) > 0) { //
/** Form submit function, file write **/
} else {
?>
<html>
<form action="#" method="POST">
<!-- Enter HTMLform here -->
</form>
</html>
<?php } ?>
This pseudocode is not pretty, but will do in terms of explaining stuff. The # in the form action means that the same script is to be called upon submission. The if(count($_POST) > 0) checks whether data has been submitted. If so, the file will be written. Otherwise, the form will be displayed.
Good luck.
I have the following script that is supposed to take the input from two forms and then rewrite two different files with the input from the forms. When I put in text into the forms and run it I get no errors but neither of the files were changed at all. I have all the proper permissions set and correct file paths. I changed the paths in the code below so as not to show sensitive information to my server. I'm really scratching my head at this one since php is a fairly new language to me so any help is greatly appreciated.
HTML Code
<form name="editfront" action="save.php" method="post">
<div class="editareasmall">
<textarea rows="1" cols="150" id="title" name="title"></textarea>
<script>$('#title').load('../content/front/Title');</script>
</div>
<div class="contentheader">Content</div>
<div class="editareabig">
<textarea rows="30" cols="150" id="content" name="content"></textarea>
<script>$('#content').load('../content/front/Content');</script>
</div>
<input type="submit" value=" Save " class="save">
</form>
PHP Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$title = $_POST['title'];
$content = $_POST['content'];
$filenametitle = "../content/front/Title";
$filenamecontent = "../content/front/Content";
echo 'Form data has been saved';
$filetitle = fopen($filenametitle, "w") or die("can't open file");
$filecontent = fopen($filenamecontent, "w") or die("can't open file");
fwrite($filetitle,$title);
fwrite($filecontent,$content);
fclose($filetitle);
fclose($filecontent);
?>
When you submit the form, do you get your output message?
"Form data has been saved"
Are you sure that in php multi file opening is available?
Change your error reporting to -1. You will be informed about notice error.
I have developed a text box and I am trying to write this data to a text file.
The PHP code is generating the file but the data is not being written.
below is my code:
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
<?
$a=#$_POST["text_box"];
$myFile = "t.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,$a);
fclose($fh);
?>
the biggest issue i see with your code is the fact that you aren't opening php tags. you do
<?
but it should be
<?php
then, the way you call $_POST and write file and stuff means it will be executed when you first load the form into the browser as well. the php engine makes no distinction between first run and consecutive run. this means that even if the user don't submit anything, there will still be an empty file, created from the run of the script where the form was displayed. it's a side effect. i've modified your code just a little. here's my take on 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>
</html>
<?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);
}
?>
The form's submit action is "get", but in your PHP code, you get the variable by $_POST. Try by $_GET instead.
The form is sending a GET request, but you are trying to access $_POST["text_box"]. Try changing that to $_GET['text_box'], or using a form method POST instead.
You can replace your PHP code with just:
if ($_REQUEST) {
file_put_contents("t.txt", $_REQUEST["text_box"]);
}
That will ensure that file only gets overwritten when the form is actually submitted, not also when the form is displayed.
Does $a have data? Try echo'ing it out. Or, try print_r'ing your $_POST.
EDIT: Your form's method is get, but you're trying to use $_POST. Use $_GET, or, $_REQUEST.