Jump to content

Recommended Posts

I've done searches on google (also tried finding other posts on this forum but my searches are too generic) and it got me started on how to access existing files but I can't seem to find out how to make a new file using the same contents of that file. And once I do that I also need to edit a line of code that holds a variable that is associated to the database.

 

Basically I have a form set up for a user to make a new page and based on some options it will select a template and make a brand new page. I then need to edit a couple of lines in that document. Both of these I don't know how to do. Google just keeps leading me back to how to open files.

 

All i have set up so far is (as far as opening and getting ready to modify the page)

<?php
case '1':
			$frontend_filename = '../templates/three_cols.php';
			$backend_filename = 'templates/three_cols.php';
			$frontend = fopen ($frontend_filename, "r+");
			$backend = fopen ($backend_filename, "r+");
			break;
?>

 

Any and all help is appreciated,

Thanks,

Justin

 

**edit

if anyone has links to tutorials that would be good too, I dont mind doing tutorials, I just cant find one...

 

(I know there are other ways to achieve a similar effect like storing the template data in a database, but I have to do it this way for a number of reasons).

let me see. That's a templating system. pretty straight-forward on the surface. take a template file:

templatefile.tpl

<html>
    <head>
        <title>::TITLE::</title>
    </head>
</html>

and parse it, creating a new file:

<?php
$file = "templatefile.tpl";
$title = "New title!";
$fh = fopen($file, "r");
$data = fread($fh, filesize($fh));
fclose($fh);
$page_with_title = str_replace("::TITLE::", $title, $data);
//put the new page in a database, or dump it to a file, or echo it here:
echo $page_with_title;
?>

let me see. That's a templating system. pretty straight-forward on the surface. take a template file:

 

whoops, um I used template there very loosly, sorry. it's just a php file and i stuck it in a folder named templates ^_^

but I might be able to use some of that code you provided already.

 

<?php
$file = "myfile.php";
$title = "New title!";
$fh = fopen($file, "r");
$data = fread($fh, filesize($fh));
fclose($fh);
$newfile = "newfile.php";
$fh = fopen($newfile,"w");
$data = str_replace("::TITLE::", $title, $data);
fwrite($fh,$data);
fclose($fh);
?>

would the above work?

I tested out the above (to some extent, I changed it to match my application more, but the same basic code is there) and I got:

 

Page not found

The requested URL /beta/admin/test.php was not found on this server.

 

It's having problems making the files so I'm guessing there is a server setting I need to set up?

 

It can find and read the "template" files I have made already. I added the "or die" to the fopen()'s that were trying to make the new files and it showed up but had no problem with the fopen()'s that were reading the existing files.

 

ok i changed the folder permissions to 755 and now the page is created but I get a blank screen (it's not getting passed the code) so here it is:

 

<?php
//new files
		$newpage = "$page_link".".php";
		$fh = fopen("../$newpage","w") or die ("couldn't make file");
		$fh2 = fopen("$newpage","w") or die ("couldn't make file2");
		//replace content in the data
		$data = str_replace('0000000000',"$pageid",$data);
		$data2 = str_replace('0000000000',"$pageid",$data2);
		//Proper navigation heading?
		switch($parent_page){
			case '1': //home
				$data = str_replace('<body>','<body id="page1">',$data);
				$data2 = str_replace('<body>','<body id="page1">',$data);
				break;
//etc etc....
		}
		//write the data into the files
		fwrite($fh,$data);
		fwrite($fh2,$data2);
		//close files
		fclose($fh);
		fclose($fh2);
?>

further troubleshooting has lead to $data/$data2 being empty, code for this:

<?php
switch($layout){
			case '1':
				$filename = '../templates/three_cols.php';
				$filename2 = 'templates/three_cols.php';
				break;
			case '2':
				$filename = '../templates/two_cols.php';
				$filename2 = 'templates/two_cols.php';
				break;
			case '3':
				$filename = '../templates/three_separate_cols.php';
				$filename2 = 'templates/three_separate_cols.php';
				break;
			case '4':
				$filename = '../templates/one_col.php';
				$filename2 = 'templates/one_col.php';
				break;
		}
		//open files for reading
		$fh = fopen ($filename, "r") or die ("couldn't open file");
		$fh2 = fopen ($filename2, "r") or die ("couldn't open file");
		//get data from files
		$data = fread($fh,filesize($fh));
		$data2 = fread($fh2,filesize($fh2));
		//close files
		fclose($fh);
		fclose($fh2);
?>

Are the paths supposed to be absolute or full path on server? The relative paths are correct. From the examples I've seen it's relative...

Found the problem (for those who may find it useful)

<?php
$data = fread($fh,filesize($fh));
         $data2 = fread($fh2,filesize($fh2));
?>

should be:

<?php
$data = fread($fh,filesize($filename));
         $data2 = fread($fh2,filesize($filename2));
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.