Forum Moderators: coopster

Message Too Old, No Replies

Open and write to a defined location of a given file

can it be done?

         

henry0

9:21 pm on Dec 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am quite close of finishing my own templating system to allow an user creating its own set of web pages within a sector of a given site
last leg:
I need to figure how to open a file and find a way to have the open and write script finding where is
<? $ID=0000?>
and replace 0000 with a value that I will pass through an array and a query for the new ID finding the new ID and passing it is no problem
but opening the file and working at a precise location .. well I don't know how to achieve that

thank you

regards
Henry

lorax

5:42 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you need something more than fopen [us3.php.net]?

henry0

5:49 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



well yes and no :)
for some reason I have a hard time understanding
how to define a location (precise location) where writing needs to be performed

Henry

mogwai

5:56 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



Hi,

If I understand this correctly you would like to open a template file and replace specified place holders with values from your script, if so try:

- Use fopen to open the file.
- fread the contents of the file into a variable (eg $file_contents)
- Use str_replace to replace the place holder with the value:

$carrots = str_replace("<? $ID=0000?>", $value_to_insert, $file_contents);

If you want to replace multiple values str_replace() will accept arrays as the $search, $replace values so you could do something like:

$sprouts = str_replace(
array ('<? $ID=0000?>', '<? $ID=0001?>'),
array ($value_to_insert_1, $value_to_insert_2),
$file_contents);

The results of your str_replace will give you the populated template which you can do with as you wish.

Hope this helps

lorax

6:04 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



for some reason I have a hard time understanding - how to define a location (precise location) where writing needs to be performed

There are a number of things that can go wrong so I'll take a stab at one of the more common problems - file paths.

The simplest way to think of this is in terms of the webserver. The script is running on the server and therefore uses paths in terms of the server - not just your webspace. HTML is handled by the webserver service and it understands and works with paths in terms of URIs. PHP on the other-hand is PRE-hypertext processing and the PHP engine is working with files according to the webservers dir/file structure - not URIs.

So while within your webspace you may refer to a document in a sub-directory called "docs" as "/docs/thedocument.html" for your links, the webserver may see it as "/home/user/yourwebspacename/docs/thedocument.html".

The easiest way to determine what the webserver is using for a path to that location is to put a phpinfo file in the directory and call it. In the results page you'll find the path to the phpinfo file.

Way off base?

[edited by: lorax at 6:08 pm (utc) on Dec. 29, 2003]

henry0

6:06 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks exactly what the doctor recommended
I had a hard time figuring it out

Lorax, no you are not off path, I was indeed going to deal server side
my real problem (now figured) was to figure how precisely write to a defined level
but the above post addresses that

henry0

4:59 pm on Jan 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I always receive great help from the WebmasterWorld
So here is my little humble contribution regarding my last quest
Thanks

Henry

<<<<
// what we do: copy a template, rename it (unique name based on ID)
// template is includes driven, we need a single set of includes for any number of files created upon template model
// so we use a master ID # that drive the whole new file, we need to grab it and insert it in the new created file

// first we look for ID ... where Username and Password etc...

<?
function db_connect()
{
$result = @mysql_pconnect("localhost", "root", "");
if (!$result)
return false;
if (!@mysql_select_db("flba"))
return false;

return $result;
}
?>

<?php
$conn = db_connect();
?>
<?
$query = "select id from YOUR TABLE where username= '$username' and password= '$password' ";
$result= mysql_query ($query);
if ($username && $password == ""){
echo"<b>Missing field try again</b>";
}
else
{
echo"<b> ECHO WHATEVER! IF NEEDED</b>";
}
// We copy old file (here index.php) and rename it with a unique name here we use a combo of index and the ID value
//ID is provided through my_sql_fetch_array
$old_file="index.php";
while($query_data=mysql_fetch_array($result)) {
$new_file="index".$query_data["id"]. ".php"; }

rename($old_file, $new_file);

?>

//we open the new file and set a pointer where we want the ID to be written

<?
// if needed add: chmod up to 0666
$fp = fopen("./$new_file", "r+");
if(!$fp) die ("Hummm cannot open file");

//105 equal bite # from where new written data will be inserted
fseek($fp, 105);
// passs id # to new file $id
$query = "select id from YOUR TABLE where username= '$username' and password= '$password' ";
$result= mysql_query ($query);

while($query_data=mysql_fetch_array($result)) {


$byte = fwrite($fp,$query_data["id"]);
}
?>

// do some check up and security additions if needed
//then:
<?
echo"Your Home Page has been Created, please ...............";

?>

>>>>