Jump to content

Fix so includes don't have to be absolute?


The14thGOD

Recommended Posts

I created a CMS that makes new pages from existing pages. It makes almost exact duplicates except that an ID is added and a variable is changed. I downloaded the created files  and they are perfect duplicates of the pre-existing files. The problem is it would ignore my includes, so I changed them to absolute paths and it worked.

 

Now I'm glad that it works but I want to know why the includes have to be absolute paths? They are in the same folder as the rest of the pages using the same lines so it's not a path issue? The code is identical. On top of this my Database calls have to be put directly into the file (thus defeating the include purpose...).

 

Is there any kind of correction I need to put into the following code so I don't have to make them absolute paths? Or is there a different way to create pages from existing ones than the method below?

<?php
import_request_variables('pg');
if($send=='send'){		
	include('db connection script');
	//Insert data into databases
	$newpage = "$page_link".".php";
	$query = "a query') ";
	mysql_query($query);
	include('includes/random_gen.php');
	$pageid = get_rand_id(10);
	$query2 = "a query";
	mysql_query($query2);		
	//create new files
		//create new pages, front end page, back end page
		switch($layout){
			case 1:
				$filename = '../templates/three_cols.php';
				$filename2 = 'templates/three_cols.php';
				break;
			//etc etc
		}
		//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($filename));
		$data2 = fread($fh2,filesize($filename2));
		//close files
		fclose($fh);
		fclose($fh2);
		//new files
		$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">',$data2);
				break;
			//etc
		}
		//write the data into the files
		fwrite($fh,$data);
		fwrite($fh2,$data2);
		//close files
		fclose($fh);
		fclose($fh2);			
		//set permissions
		chmod("../$newpage",644);
		chmod("$newpage",0644);
	//end file creation
	header("Location: $newpage");
	exit(0);
}
?>

 

The include code is a normal include:

<?php include('includes_folder/file.php') ?>

 

I don't see how this would break it but does it matter if the pre-existing files are in a "templates" folder? There is no "includes" folder in there but I'm just copying the data of the files and putting it outside the "templates" folder where the include path is correct.

 

If there are any improvements that can be made please let me know, I'd really like to just use relative paths for future changes/maintenance etc.

 

Any and all help is much appreciated,

Thanks,

Justin

Link to comment
Share on other sites

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.