submit a form to .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ziggy1621
Forum Commoner
Posts: 37
Joined: Mon Sep 12, 2005 5:12 pm

submit a form to .txt file

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the question?

fopen()-fwrite()-fclose() or file_put_contents() (requires PHP 5+) may be of interest.
ziggy1621
Forum Commoner
Posts: 37
Joined: Mon Sep 12, 2005 5:12 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ziggy1621
Forum Commoner
Posts: 37
Joined: Mon Sep 12, 2005 5:12 pm

Post 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]
Last edited by ziggy1621 on Mon Feb 20, 2006 4:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if the server is a Windows one, you may need to use 'wt' or 'wb' for the mode (second parameter)
ziggy1621
Forum Commoner
Posts: 37
Joined: Mon Sep 12, 2005 5:12 pm

Post 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
Post Reply