Page 1 of 1

submit a form to .txt file

Posted: Mon Feb 20, 2006 2:41 pm
by ziggy1621
hello all,

I'm trying to set a form to submit to a text file and replace the old text file.

I.E. text file is file1.txt. which has the following code in it:

Code: Select all

news=subject;brief content
url=www.your_url.com
This is a file that a scrolling news ticker works off of. I'm trying to make it a little easier to update by setting the client up with a form he can go to and put in the new info and submit. Once it is submitted, it would be in the format above and would replace the old text file.

thanks in advance for any help,

ziggy

Posted: Mon Feb 20, 2006 2:50 pm
by feyd
what's the question?

fopen()-fwrite()-fclose() or file_put_contents() (requires PHP 5+) may be of interest.

Posted: Mon Feb 20, 2006 3:16 pm
by ziggy1621
feyd wrote:what's the question?

fopen()-fwrite()-fclose() or file_put_contents() (requires PHP 5+) may be of interest.
Thanks for the response... I'm trying the fopen() method and getting errors of "failed to open stream". My clients site is hosted, so I have no access to permissions...

Am I stuck?

ziggy

Posted: Mon Feb 20, 2006 3:27 pm
by feyd
How are you using fopen()? If you placed a URL in the filename, fopen() cannot write to a URL. If the script is local to the server that needs changing, and you've referenced the file using local system pathing, PHP may not be allowed to write to the file or directory.

Try having PHP create the file (or a new file to test). If it can continue making further edits to the file afterward then PHP doesn't have permission to write/execute over the original file. If it continues to fail, PHP likely doesn't have permission to write/execute inside that directory.


There are a few alternates to fopen(). One that springs to mind is FTP. PHP can use its FTP functions (provided you have access to them) to upload a new version of the file to itself. Although more involved, it can get around some permissions issues.

Posted: Mon Feb 20, 2006 4:46 pm
by ziggy1621
I'm guessing the FTP option is going to be best. You mean inserting it as "ftp://username:password@domain.com/publ ... /file1.txt". Below is the code I'm using. Obviously the REQUESTS are from the form.

EDIT: or is this what you are talking about?

Code: Select all

<?

$subject=$_REQUEST['subject'];
$description=$_REQUEST['description'];
$link=$_REQUEST['link'];

$file = 'file1.txt';
$stuff = "name=$subject;$decription;/n url=$link";

$openfile = fopen($file, 'w');

fwrite($openfile, $stuff);

fclose($openfile);

?>
think I'm on the right track.


thanks,

ziggy[/url]

Posted: Mon Feb 20, 2006 4:52 pm
by feyd
if the server is a Windows one, you may need to use 'wt' or 'wb' for the mode (second parameter)

Posted: Mon Feb 20, 2006 4:56 pm
by ziggy1621
feyd wrote:if the server is a Windows one, you may need to use 'wt' or 'wb' for the mode (second parameter)
No go muchacho on the wt / wb.

Is this what you were talking about for ftp commands? HERE