When a user clicks submit I am trying to create a file.
Centos 7
php 7.2
I have tried to modify the code a couple different ways also tried file_put_contents does not seem to work.
Code:
index.php
<form action="sendmail.php" method="post">
<input type="text" placeholder="fname" name="fname">
<button type="submit">submit</button>
</form>
sendmail.php:
<?php
$fname = $_POST["fname"];
echo "bef: " . $fname;
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
echo "aft: " . $fname;
$txt = $fname;
fwrite($myfile, $txt);
fclose($myfile);
?>
Directory to create file
[root#webserver webdir]# ll -d /signupemails/
drwxrwxrwx. 2 apache apache 51 Aug 25 20:41 /signupemails/
If i change sendmail.php and hardcode the $fname and run php sendmail.php it will create the file fine
Creating it from the browser I get "Unable to open file!"
Your code should work as is, i just tested it and it seemed to work fine.
Is selinux disabled?
Try changing this line:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
PHP doesn't have the same logical "or" semantics as JavaScript. That expression returns a boolean, so $myfile will equal true instead of the file handle.
Write it this way:
$myfile = fopen("/signupemails/" . $fname . ".txt", "w");
if (!$myfile) die("Unable to open file!");
I suggest do with js
For example
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("hello.txt","This is the content of my file :)");
Related
I used below HTML code and php code to Write new files.
<html>
<body>
<form action="write.php" method="get">
ID : <input type="text" name="ID"><br>
<input type="submit">
</form>
</body>
</html>
<?php
$myfile = fopen($_GET["ID"] , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
?>
If I submit TEST to html form , php code writes a file named "test". it hasn't a file extension.
How to write "TEST.html" with above code.?
Add the extension to the filename before opening the file:-
$filename = $_GET["ID"] . '.html';
$myfile = fopen($filename, "w") or die("Unable to open file!");
Note You are opening up yourself to all kinds of security issues here.
just use this code:-
fopen($_GET["ID"].'.html' , "w")
You need to add .html with a !empty() check there, like below:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html" , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
}
?>
To open and read this file:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html", "r") or die("Unable to open file!");
echo fread($myfile,filesize($_GET["ID"]));
fclose($myfile);
}
?>
OR:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$file = fopen($_GET["ID"]."html","r") or die("Unable to open file!"); ;
while(! feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
?>
Note:-
In my opinion you need .txt rather than .html.An HTML file with text like test really have no mean, or server any useful purpose. BTW it's up-to-you what extension you want. I just gave my opinion.
I'm creating a tool like Pastebin for train myself in PHP (I'm a beginner yet).
Everyone without log in/sign up can create .txt file and share it easily.
Anyway, I'm facing a problem:
How can a guest edit the .txt file when he created it?
<?php
$titleFile = $_POST['title'];
$textFile = $_POST['text'];
$newFile = fopen($titleFile . ".txt", "w") or die("Unable to open file!");
fwrite($newFile, $textFile);
$url = $titleFile . ".txt";
header("Location: $url");
?>
http://mattewdev.altervista.org/
I have a PHP script that looks like:
if(file_exists("temp.txt")){
$myfile = fopen("temp.txt", "a") or die("Unable to open file!");
}
else{
$myfile = fopen("temp.txt", "w") or die("Unable to open file!");
}
date_default_timezone_set("America/New_York");
$date = date('m/d/Y h:i:s a', time());
$text = $date . PHP_EOL;
fwrite($myfile, $text);
fclose($myfile);
When I run the above script in Powershell using
php myscript.php
A text file is created and written into.
If I try to run the same file with
Start-Job ScriptBlock {php myscript.php}
I'll get a response like
But my text file is never written into. It seems like Start-Job never starts my PHP script.
How do I get Start-Job to start running PHP scripts?
Ok I'll write an answer about all of this:
From test (with your script as origin) I added some echo to get something in Receive-Job in powershell.
It was obvious the script was running as intended, but not in the expected directory.
So I added a getcwd() within an echo and this show me the working dir for the job (or scriptblock) is my documents directory) and I found the file there wihtin my home\documents.
Here the final script I eneded with:
echo "I'm running in ".getcwd()." !\n";
if(file_exists("temp.txt")){
$myfile = fopen("temp.txt", "a") or die("Unable to open file!");
}
else{
$myfile = fopen("temp.txt", "w") or die("Unable to open file!");
}
echo "Ok, file was opened: \n".print_r(fstat($myfile),1)."\n";
date_default_timezone_set("America/New_York");
$date = date('m/d/Y h:i:s a', time());
$text = $date . PHP_EOL;
$r=fwrite($myfile, $text);
echo "Write in file returned $r \n";
fclose($myfile);
echo "File closed\n";
And that is the output is with Powershell:
Start-Job -ScriptBlock { D:\wamp\bin\php\php5.4.3\php.exe D:\wamp\bin\php\php5.4.3\test.php }
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
51 Job51 Running True localhost D:\wamp\bin\php\php5....
And retrieving the output:
Receive-Job 51
I'm running in C:\Users\my_name\Documents !
Ok, file was opened:
Write in file returned 24
File closed
Hope it will help.
So I'm working on a little php file that is supposed to alter a specific file for the user. It gets the contents of the file and puts them into a textarea within a form. How can I make it so any edits done within this textarea will be rewritten to the file on the server? And even better, would I be able to allow the user to only edit certain lines, and have only those lines be rewritten?
Here's my code so far:
<?php
$filename = "../tree_c/index.php";
//$fp = fopen ($filename, "w"); <- doesn't seem to work for it opens empty file.
$contents = file_get_contents($filename);
/*
if (isset($_POST['field'])) {
// something here to rewrite the file.
*/
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<textarea name="field"><?php echo $contents ?></textarea>
<input type="submit" value="Save">
</form>
This should work fairly easily:
if (isset($_POST['field'])) {
file_put_contents($filename, $_POST['field']);
}
$datafile = "Files.txt";
$fp = fopen($datafile, "r");
$textdata= fgets($fp, 1024);
$text = '"'.$textdata.'"';
$this->set('text',$text);
if(!empty($this->data))
{
$datas = $this->data['data']['text']; //(your Textarea name)
$myFile = "Files.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $datas);
fclose($fh);
}
hope this ll help you....
I am completely new to php. And have been following tutorials. I need to accomplish making a form which calls a php function which takes the input of the form and writes it to a file. Should be easy enough? I have tried this in many ways and this is the close I have gotten. However for some reason it writes works three times meaning three entries imputed then everything after the third is ignored. test.txt is the file which I write to.
<?php
$email= $_POST['email'];
$myFile = "test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$email\n";
fwrite($fh, $stringData);
fclose($fh);
?>
Here is the form I use to use the php.
<div id="login-box">
<form name="form" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<div class="text-field">
<input name="email" id="email" type="text">
</div>
<input id="login" type="submit" value="Submit">
</form>
</div>
I have tested your code and there is not any problem whatever you are mentioning.The code is inserting more than three record.So please carry on ...
Try to use this example
<?php
$email= $_POST['email'];
$myFile = "test.txt";
// First, let's make sure that the file exists and is writable.
if (is_writable($myFile)) {
// In our example we're opening $ myFile in the "append".
if (!$handle = fopen($myFile, 'a')) {
echo "Can't open ($myFile)";
exit;
}
if (fwrite($handle, $email) === FALSE) {
echo "Can't wtire to file ($myFile)";
exit;
}
echo "OK. Content ($email) written to file ($myFile)";
fclose($handle);
} else {
echo "File $myFile not available for writing.";
}
?>
Also you can simply debug your php code using this code:
echo <something_what_you_need>; die;
Maybe your code called multiple times and you can't see it.
Your Code should work but you wrote <? and not <?php in your form's action attribute.
Add these 2 lines at the top of your PHP code
ini_set('display_errors',1);
error_reporting(E_ALL);
and see the real error message, not your silly "can't open" but the explanation, what is certainly going wrong.
If you can't get it's meaning yourself, post it here, exact and whole.
Maybe you submit form 3 times or push reload button on your browser panel ?
can you post your html ?
and try this:
<?php
if($_POST['email'])
{
$email= $_POST['email'];
$myFile = "test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$email\n";
fwrite($fh, $stringData);
fclose($fh);
}
?>