Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can i write directly to a html file on a server 2

Status
Not open for further replies.

nugget

Programmer
Apr 13, 2001
11
0
0
AU
Hi...what i need to do is have a web page that can be easily updated by the client(say one frame). I thought that if i could set up like an admin page with a form on it and then write the info in that variable straight into a html page on the server...Is this possible!!! or do you have to use a dbase??
Any help or suggestions would be much appreciated..thankyou.
Dave.
 
This is possible to do using the file system commands

But I do not recommend doing it this way. One or two pages this way would not be too bad, but it could quickly turn into a mess if the number starts to grow. This can also become a hassle if your pages ever change (mine do on a weekly basis). Since you are 'blindly' editing a HTML page with a script you must know the exact line numbers to change. When you edit a page's layout, metta tags, etc you have to also edit all PHP scripts to reflect what line numbers your user text was moved to.

What I would recommend is one of the following.

If you have access to a DB that PHP can query (MySQL works very well) -- store the user input into the DB and have PHP extract the text to be displayed. This way is extreamly handy if you have multiple 'user' pages that have the exact same layout, but different text/images/colors/etc. You would then only need to write a single PHP script for all users. When you wanted to change your site layout, you only need to edit one page for all users instead of one page per user.

The second way is if you dont have access to a DB, you could effectivly store the user input into plain text files. Your PHP script would then "include" the text of these files in the desired location. This way is less desirable, but would eliminate the line number problems of editing a page directly.
 
thanks for the advice hockey..the database stuff is a bit above me at the moment..to be able to edit a text file would suit what i want to do perfectly..if i could store a string of say 100 chrs and then display it in the page would be great.
If you could point me a little more in the right direction on this would be much appreciated. I imagined doing this with the write function to add text to the txt file..is this the way to go??

Thanks heaps Dave.
 
Here is a quick little example of what I would do. Beware that the following code is just for example... I have put in no error correction, security checking, etc, and have only ran one test on each script to make sure that the basics work. Also, I use Unix (FreeBSD to be exact), so if you are running in Billy's platform (Windows), you may have to change things to get them to work. Here is a list of the PHP functions that I have used:
include() fopen() fread() filesize() fwrite() header()
I would highly recommend reading the whole Filesystems functions chapter in the PHP manual for such a project. Understanding how PHP can fully interact with files is very usefull .... In fact I recommend reading the entire PHP manual front to back {RTFM :)}. For the most part it is fairly well written, and even if you dont understand everything it talks about, you will at least know a summary of everything PHP is capable of --- which is a lot.

Anyways ... here is my attempt to describe how I would approach this project. see next post
 
User Text files:
jim.txt
frank.txt
lisa.txt

User’s Page:
users.php

Links to user’s page:
<a href=”users.php?user=jim&quot;>Goto Jim’s Page</a>
<a href=”users.php?user=frank&quot;>Goto Frank’s Page</a>
<a href=”users.php?user=lisa&quot;>Goto Lisa’s Page</a>

User’s Input page: password protected
userselect.html -- user selects their name
userinput.php – user changes their text
userparse.php – script that changes user text and sends them to their edited page

CODE:
users.php
Code:
<HTML>
<BODY>
Enter your HTML layout here
Enter the following code where you want the users text to be placed … recommended to place inside a table.
<?
include (“/path/to/file/$user.txt”)
?>
</BODY>
</HTML>
userselect.html
Code:
<HTML>
<BODY>
Enter your HTML layout here
<form action=”userinput.php” method=”Post”>
<SELECT NAME=&quot;user&quot;>
	<OPTION VALUE=jim>JIM</OPTION>
	<OPTION VALUE=frank>FRANK</OPTION>
<OPTION VALUE=lisa>LISA</OPTION>
</SELECT>
<input type=&quot;submit&quot; name=&quot;Goto User’s Text&quot; value=”submit”></FORM>
</BODY>
</HTML>
userinput.php
Code:
<?
$filename =&quot;/path/to/files/$user.txt&quot;;
$fp = fopen($filename, &quot;r&quot;);
$usertext = fread ($fp, filesize ($filename));
?>
<HTML>
<BODY>
Enter your HTML layout here
<form action=&quot;userparse.php&quot; method=&quot;Post&quot;>
<textarea name=&quot;usertext&quot; ROWS=13 COLS=55 WRAP=&quot;VIRTUAL&quot;><?=$usertext?></TEXTAREA>
<input type=&quot;hidden&quot; name=&quot;user&quot; value='<?=$user?>'>
<input type=&quot;submit&quot; name=&quot;Edit Text&quot; value=&quot;submit&quot;></FORM>
</BODY>
</HTML>
userparse.php
Code:
<?
$filename =&quot;/path/to/files/$user.txt&quot;;
$fp = fopen($filename, &quot;w”);
fwrite($fp, $usertext);
header (&quot;Location: [URL unfurl="true"]http://www.domain.com/users.php?user=$user&quot;);[/URL]
?>
 
well what can i say ... the stuff you gave me was great and using this i got the pages to update perfectly..this is the site that i was working on and now i can update through the admin page..they update only 5 pages ..juniors,match reports,overseas activities and family fun.

i didnt end up using the include function i did it like this ..to display the contents of the file..(include actually looks much easier )

<?php


$FILE = fopen(&quot;ad/body_text.txt&quot;,&quot;r&quot;);
if($value = fgets($FILE,9999))
{ $value = trim($value); }
fclose($FILE);

$FILE = fopen(&quot;ad/heading.txt&quot;,&quot;r&quot;);
if($HEADING = fgets($FILE,999))
{ $HEADING = trim($HEADING); }
fclose($FILE);
?>
<font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;1&quot; color=&quot;#333333&quot;><font size=&quot;2&quot;><span class=&quot;royals_grey_text&quot;><b> <?echo $HEADING ?></b>
<?echo $value ?>
</td>

---------------

there is however one problem....how do i get the string to recognise carriage returns???
i have been telling them to insert html tags to simulate a return!!!!( a bit bodgy but it works).

once again..thanks heaps for the help..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top