FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


does someone have a simple "save to txt file" scri





m-productions
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.
[FuN]goku
ill try it for you just gimme a few mins... im doing like 20 things at once lol.
[FuN]goku
right heres the page with the form.
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';
}
?>
m-productions
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.
[FuN]goku
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.
hexkid
[FuN]goku wrote:
then action.php
Code:

<?php
$text = $_POST['textfile'];

if($text == ""){
// ...
?>

To avoid textareas with a single (or several) ENTER I'd rather test with
Code:
if (trim($test) == "") { /* ... */}

Good code anyway Smile
m-productions
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.
hexkid
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.
m-productions
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
dotapinoy3480
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>


hey!!! this one is easier
m-productions
ok this just isnt my day..im getting php error after error... ok this code use to work.. but i guess it dosnt now...


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
kv
You have to use absolute path, not the http path. If your home directory is /home/m-productions, then

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
Create a txt file
simple webserver on cable line.
need php help save command
Does someone offer .Com redirection for FRIH$?
Creating a .txt file
.txt file to .php Lined to tables
Save file on local floppy (perl)
Displaying .txt file on web page
I can't open directly .txt file on my desktop
Ip Black list
Saving a .txt file to a ftp using vb6. i need help please
VB 6.0 need help loading a .txt file into a text box
Html Forms help please?
is it possible to get a html text box to load a .txt file?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.