Local World IT - sydney web design
Home About Us Contact Us Portfolio  
Sydney Web Site Hosting
 
Sydney Website Hosting
Support Web Site Design Web Site Hosting Business Systems Networking Services
  

fopen, fwrite, and fclose

The following script demonstrates the use of these three functions. It will attempt to read from a file data.txt and then display the contents. In this case it's just simple URL's, but could be modified for use with many other applications. To see this script in action click here.

urls.php

1. Here the script takes the two variables $url and $description and then writes the data contained in them to the text file data.txt.

<HTML>
<HEAD><TITLE>Storing URL's in an external file</TITLE></HEAD>

<BODY bgcolor="#556528" text="#FFFFCC">
<TABLE width="100%" height="6" bgcolor="#FFCC00" border="1"><TR><TD><CENTER><font face=Arial color="#168820">

<H1>Useful Links Storage</H1>

</font></center></td></tr></table>

/*Function WriteToFile takes two arguments--URL and Description--which will be written to an external file.*/

<?php
function WriteToFile ($URL, $Description){
$TheFile="data.txt";
$Open=fopen($TheFile,"a");
if ($Open){
fwrite ($Open,"$URL\t$Description\n");
fclose ($Open);
$Worked=TRUE;
}else{
$Worked=FALSE;
}
return $Worked;
}

//End of WriteToFile function.

2. It then looks for data.txt and checks the data contained in it. It looks for the "\" symbol and splits the data accordingly. If it can't find data.txt, it display's "Unable to read from file".

/*Function ReadFromFile displays all the information in the external file.*/

function ReadFromFile (){

$TheFile="data.txt";
?>


<font color="#FFFF00">
<?php
$Open=fopen($TheFile,"r");
if($Open){
print ("<strong>URL's currently listed in the data file:</strong><P>\n");
$Data=file($TheFile);
for($n=0;$n<count($Data);$n++){
$GetLine=explode("\t",$Data[$n]);
print("$GetLine[0]<BR>\n$GetLine[1]<P>\n");
}
fclose($Open);
print("<HR><P>\n");
}else{
print("Unable to read from data.txt!<BR>\n");
}
}

?>

//End of ReadFromFile Function.

3. The createform( ) function will now generate the form used to input the data to the variables $url and $description. A hidden variable called $beensubmitted is also created. You will find out why later.

/*Function CreateForm will display the html form.*/

<?php
function CreateForm(){


?>

<font color="#BBFF22">
<?php
print("<strong>Add a URL to the data file:</strong>\n");
print("<FORM action=\"urls.php\" METHOD=post>\n");
print("URL<INPUT type=text name=\"Array[URL]\" size=60><BR>\n");
print("Description<TEXTAREA name=\"Array[Description]\" rows=5 cols=40></textarea><BR>\n");
print("<INPUT type=hidden name=\"BeenSubmitted\" value=\"TRUE\">\n");
print("<INPUT type=SUBMIT name=\"SUBMIT\" value=\"Submit!\"></FORM>\n");
}


//End of the CreateForm function

4. The handleform( ) function will then process the data passed to it via the form created by the createform( ) function.

//Start of the handle form function

function HandleForm(){
global $Array;
$Pattern="(http://)?([^[:space:]]+)([[:alnum:]\.,-_?/&=])";
if(eregi($Pattern, $Array["URL"])){
$Replace="<a href=\"http://\\2\\3\" target=\"_new\">\\2\\3</a>";
$Array["URL"]=eregi_replace($Pattern,$Replace,$Array["URL"]);
$CallFunction=WriteToFile($Array["URL"], $Array["Description"]);
if($CallFunction){
print("<strong> Your submission-- $Array[URL] --has been received!</strong><P>\n");
print("If neccesary, scroll down to the end of the list<P><HR><P>\n");
}else{
print("Your submission was not processed due to a system error!<BR>\n");
}
}else{print("Please enter a valid web address!\n");
}
}


//End of the HandleForm function

5. This next statement determines weather to process the data contained in $url and $description as well as reading from the data.txt file, or just read the data.txt file. This is where the script uses the variable $beensubmitted. It uses this variable to determine if there is data to process or not.

/*This next conditional determines whether to handle the form, depending upon weather or not
$BeenSubmitted is true.*/


if($BeenSubmitted){
HandleForm();
}
ReadFromFile();
CreateForm();
?>

</BODY>
</HTML>




Other Articles: