I was wondering if anyone has a very simple php script that has just one field, and the Submit button, and it saves the information in the field to a .txt file.
does someone have a simple "save to txt file" scri
ill try it for you just gimme a few mins... im doing like 20 things at once lol.
right heres the page with the form.
i called it index.php
then action.php
i called it index.php
| Code: |
|
<form method="POST" action="action.php"> <p>Your Text File<br><textarea rows="19" name="textfile" cols="69"></textarea><br><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> |
then action.php
| Code: |
|
<?php $text = $_POST['textfile']; if($text == ""){ echo 'You should type something before submitting!'; }else{ $f = fopen("yourtextfile.txt","a"); fwrite($f, "$text"); fclose($f); echo 'Wrote to file'; } ?> |
thank you very much, thats what i needed, ill set it up in a few, knowing how newby i am at php i might mess it up somehow.. who knows rofl.
hehe i use to be the same... but i actually sat my ass down one day and learnt php + mysql from scripts ive been gathering and reading manuals. Now i know enough to work my way around.
| [FuN]goku wrote: | ||
then action.php
|
To avoid textareas with a single (or several) ENTER I'd rather test with
| Code: |
| if (trim($test) == "") { /* ... */} |
Good code anyway
ok i got it to work the first time, however, if you try to go back and change whats in the file (aka re submit text) it dosnt work right. I want it so that, it will always overwright whats already in the text file.
ps: yeah i need to one day just site down and learn php. There are so many times i wish i could use it. I know AS with flash, i make my sites in flash now, and ive been useing "sharedObjects" as a way to get around using php with flash, but i still need to learn it, it would just open up so many more possibiltiees.
ps: yeah i need to one day just site down and learn php. There are so many times i wish i could use it. I know AS with flash, i make my sites in flash now, and ive been useing "sharedObjects" as a way to get around using php with flash, but i still need to learn it, it would just open up so many more possibiltiees.
| m-productions wrote: |
| I want it so that, it will always overwright whats already in the text file. |
You need to change the second parameter to the fopen() call.
oh i see i see, so i want to change the "a" to a "w" thnx a lot!
it works great!! thnx, again both of yeah
it works great!! thnx, again both of yeah
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
hey!!! this one is easier
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
hey!!! this one is easier
ok this just isnt my day..im getting php error after error... ok this code use to work.. but i guess it dosnt now...
this file is located in http://beta.m-productions.us/scripts
if i make the var site = to test.txt it works fine
but if i make it = to http://beta.m-productions.us/scripts/test.txt
it gives me this error
| Code: |
|
<?php $text = $_POST['info']; $site = $_POST['site']; $folder = $_POST['folder']; if(!is_dir('./'.$folder.'/')) mkdir('./'.$folder, 0777); $f = fopen($site,"w"); fwrite($f, "$text"); fclose($f); echo 'Wrote to file'; ?> |
this file is located in http://beta.m-productions.us/scripts
if i make the var site = to test.txt it works fine
but if i make it = to http://beta.m-productions.us/scripts/test.txt
it gives me this error
| Code: |
|
Warning: fopen(http://beta.m-productions.us/scripts/test3.txt) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections. in /home/mproduct/domains/m-productions.us/public_html/beta/scripts/datmaker.php on line 9 Warning: fwrite(): supplied argument is not a valid stream resource in /home/mproduct/domains/m-productions.us/public_html/beta/scripts/datmaker.php on line 10 Warning: fclose(): supplied argument is not a valid stream resource in /home/mproduct/domains/m-productions.us/public_html/beta/scripts/datmaker.php on line 11 Wrote to file |
You have to use absolute path, not the http path. If your home directory is /home/m-productions, then
You can also use
| Code: |
|
//$site = $_POST['site']; $site="/home/m-productions/scripts/test.txt";//absolute path $text = $_POST['info']; $folder = $_POST['folder']; if(!is_dir('./'.$folder.'/')) mkdir('./'.$folder, 0777); $f = fopen($site,"w"); fwrite($f, "$text"); fclose($f); echo 'Wrote to file'; |
You can also use
| Code: |
|
$site = $_POST['site']; $site="/home/m-productions/scripts/test.txt";//absolute path $text = $_POST['info']; $folder = $_POST['folder']; if(!is_dir('./'.$folder.'/')) mkdir('./'.$folder, 0777); file_put_contents($site, $text); //simple to use api echo 'Wrote to file'; |
Related topics
