Jump to content

using php to copy and edit another php file


Slowie

Recommended Posts

hey guys its me again. what i want is for me to be able to edit php using variables and post it as a new file. now to attchieve this i am simply copying the current template into the php file and telling it to grap what variables it needs. i have successfully done this with a .css file but when im doing it with a php file its erroring at <?php as the file is already in php. what i need to know is can i have the php file look at the <?php as just text to write to a file? my code is below

 

$dir = "../Header/";
$file = "" . $branchid . ".php";
$dir = "$dir"."$file";
touch($dir);
$Handle = fopen($dir, 'w');
$Data = "
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php


//We count the number of new messages the user has
$nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['user_id'].'" and user1read="no") or (user2="'.$_SESSION['user_id'].'" and user2read="no")) and id2="1"'));
//The number of new messages is in the variable $nb_new_pm
$nb_new_pm = $nb_new_pm['nb_new_pm'];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
/* Remove margins from the 'html' and 'body' tags, and ensure the page takes up full screen height */
html, body { margin:0; padding:0;}
/* Set the position and dimensions of the background image. */
#page-background {position:fixed; top:0; left:0; width:100%; height:100%;}
/* Specify the position and layering for the content that needs to appear in front of the background image. Must have a higher z-index value than the background image. Also add some padding to compensate for removing the margin from the 'html' and 'body' tags. */
#content {position:relative; z-index:1; padding:10px;}
</style>
<head>
<title>Apollo - Data Management System</title>

<!--Links-->
<link rel="icon" type="image/ico" href="favicon.ico"></link> 
<link rel="stylesheet" media="all" type="text/css" href="/styles.css" />


</head>
<body>


<div id="Headerlogo"> <img src="/images/PaulKemp/PaulBanner.png" width="100%" alt="Smile"></div>



<div class="wrapper1">
<div class="wrapper">
	<div class="nav-wrapper">

		<div class="nav">
			<ul id="navigation">
		   		<li class="active">
					<a href="/Main/myaccount.php">
						<span class="menu-left"></span>
						<span class="menu-mid">Main Menu</span>
						<span class="menu-right"></span>
					</a>
				</li>
		   		<li class="">
					<a href="/PersonnelMessages/list_pm.php">

						<span class="menu-left"></span>
						<span class="menu-mid"><b>Messages(<?php echo $nb_new_pm; ?> unread)</b></span>
						<span class="menu-right"></span>
					</a>
					<div class="sub">
		   				<ul>
		   					<li>
								<a href="/PersonnelMessages/new_pm.php">New Personal Message</a>
							</li>
		   				
							</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>
		   		

		<?php	if (checkMasterAdmin()) { ?>
				<li class="">
					<a href="#">
						<span class="menu-left"></span>
						<span class="menu-mid">Admin</span>
						<span class="menu-right"></span>
					</a>
            	   	<div class="sub">
		   				<ul>
		   					<li>
								<a href="/Admin/UploadServices.php">New KPI</a>
							</li>
		   					<li>
								<a href="/Admin/MonthReview.php">New Month Review</a>
							</li>
		   					<li>
								<a href="/Admin/CreateUser.php">New User</a>
							</li>
		   					<li>
								<a href="/Admin/EditUsers.php">Edit User</a>
							</li>

							<li>
								<a href="/Admin/HolidayRequests.php">Authorise Holidays</a>
							</li>

							<li>
								<a href="/Backup/BackupSite.php">Backup</a>
							</li>
							<li>
								<a href="/PersonnelMessages/CompanyAnnouncement.php">Company Announcements</a>
							</li>
							<li>
								<a href="/PersonnelMessages/DeleteAnnouncements.php">Delete Announcements</a>
							</li>
		   				</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>		
							<?php } ?>
                                
		<?php	if (checkAdmin()) { ?>
				<li class="">
					<a href="#">
						<span class="menu-left"></span>
						<span class="menu-mid">Admin</span>
						<span class="menu-right"></span>
					</a>
            	   	<div class="sub">
		   				<ul>
		   					<li>
								<a href="/Admin/UploadServices.php">New KPI</a>
							</li>
		   					<li>
								<a href="/Admin//MonthReview.php">New Month Review</a>
							</li>
		   					<li>
								<a href="/Admin/CreateUser.php">New User</a>
							</li>
		   					<li>
								<a href="/Admin/EditUsers.php">Edit User</a>
							</li>

							<li>
								<a href="/Admin/HolidayRequests.php">Authorise Holidays</a>
							</li>
							<li>
								<a href="/Backup/BackupSite.php">Backup</a>
							</li>
							<li>
								<a href="/PersonnelMessages/CompanyAnnouncement.php">Company Announcements</a>
							</li>
							<li>
								<a href="/PersonnelMessages/DeleteAnnouncements.php">Delete Announcements</a>
							</li>
		   				</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>	
							<?php } ?>

				<?php	if (checkBranchOwner()) { ?>
				<li class="">
					<a href="#">
						<span class="menu-left"></span>
						<span class="menu-mid">Admin</span>
						<span class="menu-right"></span>
					</a>
            	   	<div class="sub">
		   				<ul>
		   					<li>
								<a href="/Admin/UploadServices.php">New KPI</a>
							</li>
		   					<li>
								<a href="/Admin//MonthReview.php">New Month Review</a>
							</li>
		   					<li>
								<a href="/Admin/CreateUser.php">New User</a>
							</li>
		   					<li>
								<a href="/Admin/EditUsers.php">Edit User</a>
							</li>

							<li>
								<a href="/Admin/HolidayRequests.php">Authorise Holidays</a>
							</li>
							<li>
								<a href="/Backup/BackupSite.php">Backup</a>
							</li>
							<li>
								<a href="/PersonnelMessages/CompanyAnnouncement.php">Company Announcements</a>
							</li>
							<li>
								<a href="/PersonnelMessages/DeleteAnnouncements.php">Delete Announcements</a>
							</li>
		   				</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>	
							<?php } ?>



												<li class="">
					<a href="#">
						<span class="menu-left"></span>
						<span class="menu-mid">Staff</span>
						<span class="menu-right"></span>
					</a>
            	   	<div class="sub">
		   				<ul>
		   					<li>
								<a href="/Staff/ChangePassword.php">Change Password</a>
							</li>
		   					<li>
								<a href="/Staff/mysettings.php">Change Details</a>
							</li>
		   					<li>
								<a href="/Staff/HolidaysLeft.php">Holidays Left</a>
							</li>
		   					<li>
								<a href="/Staff/profile.php">My Profile</a>
							</li>
							<li>
								<a href="/Staff/MyServices.php">My Services</a>
							</li>
															<li>
								<a href="/Viewmonthreports.php">Month Review</a>
							</li>

						</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>



				<li class="">
					<a href="#">
						<span class="menu-left"></span>
						<span class="menu-mid">Referrers</span>
						<span class="menu-right"></span>
					</a>
            	   	<div class="sub">
		   				<ul>
		   					<li>
								<a href="/Referrers/Newreferrers.php">New Referrers</a>
							</li>
		   					<li>
								<a href="/Referrers/SendReferrers.php">Send Referrers</a>
							</li>
		   				
		   				
						</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>

				<li class="">
					<a href="#">
						<span class="menu-left"></span>
						<span class="menu-mid">Holidays</span>
						<span class="menu-right"></span>
					</a>
            	   	<div class="sub">
		   				<ul>
		   					<li>
								<a href="/Holidays/RequestHoliday.php">Book Off Holiday</a>
							</li>
		   			<?php	if (checkAdmin()) { ?>
							<li>
								<a href="/Admin/HolidayAmendments.php">Amend Holidays</a>
							</li>
							<li>
								<a href="/Admin/CancelledHolidays.php">Show Cancelled Holidays</a>
							</li>
															</li>
										   					<li>
								<a href="/reports/holidayreports.php">holiday report</a>
							</li>
<?php } ?>

							<?php	if (checkMasterAdmin()) { ?>
							<li>
								<a href="/Admin/HolidayAmendments.php">Amend Holidays</a>
							</li>
							<li>
								<a href="/Admin/CancelledHolidays.php">Show Cancelled Holidays</a>
							</li>
										   					<li>
								<a href="/reports/holidayreports.php">holiday report</a>
							</li>
<?php } ?>


		   				
		   				
						</ul>
		   				<div class="btm-bg"></div>
		   			</div>
				</li>


				<li class="">
					<a href="/logout.php">
						<span class="menu-left"></span>
						<span class="menu-mid">Logout</span>
						<span class="menu-right"></span>
					</a>
				</li>


	</ul>
		</div>

	</div>


</div>
</div>

<br />

";  
fwrite($Handle, $Data); 
$Data = "Bilbo Jones\n"; 
fwrite($Handle, $Data); 
print "Data Written"; 
fclose($Handle); 

Link to comment
Share on other sites

you shouldn't have so much html in your php file.

 

i would have an external file that is the template for what you want to create, and you fopen that into a string and then str_replace markers with values in your script.

 

for example, instead of:

<span class="menu-mid"><b>Messages(<?php echo $nb_new_pm; ?> unread)</b></span>

you'd have something like:

<span class="menu-mid"><b>Messages(/*-msgunread-*/ unread)</b></span>

in a template file.

 

then you open that template file, and replace the placeholders with the data

<?php
$data = fopen(template.htm, "r") // with the appropriate checks of course
str_replace( '/*-msgunread-*/', $nb_new_pm, $data);
//etc, str_replace your vars
$handle = fopen(dest_file.htm, 'w'); // with the appropriate checks of course
fwrite($handle, $data); // with the appropriate checks of course
?>

and then write the new file.

 

if it's php, make it php - but you are writing a lot of html and if you write the vars into a static file why make it php?

 

 

Link to comment
Share on other sites

ok ive made this work in one way by having it include the php file instead but how would i edit the data in the newly created file?

 

$header = include("../Header/headerj.php");
$dir = "../Header/";
$file = "" . $name . ".php";
$dir = "$dir"."$file";
touch($dir);
$Handle = fopen($dir, 'w');
$Data = "" . $header . "";  
fwrite($Handle, $Data); 
print "Data Written"; 
fclose($Handle); 

 

if i include this file and change some of the things in the original php file whats being included to variables would it pull the variables from the current php file?

Link to comment
Share on other sites

on your advice i did this

 

$banner = "/images/" . $name . "/Banner.png"; 
$data = fopen("../Header/Template.php", "r"); // with the appropriate checks of course
str_replace( '/*-headerbanner-*/', $banner, $data);
//etc, str_replace your vars
$handle = fopen("../Header/" . $name . ".php", 'w'); // with the appropriate checks of course
fwrite($handle, $data); // with the appropriate checks of course
print "Data Written"; 
fclose($handle); 

 

however the file generated only has "Resource id #12" inside.

whats happening with it?

Link to comment
Share on other sites

i'm sorry, change

 

<?php
$data = fopen("../Header/Template.php", "r"); // with the appropriate checks of course
str_replace( '/*-headerbanner-*/', $banner, $data);
?>

 

to:

<?php
$template = fopen("../Header/Template.php", "r"); // with the appropriate checks of course
$data = str_replace( '/*-headerbanner-*/', $banner, $template);
?>

 

and wrap your code in <?php ?> tags, as it makes it easier to read.

Link to comment
Share on other sites

ah thank you ive tried this and yet still im getting the error :( my code is

 

<?php
$banner = "/images/" . $name . "/Banner.png"; 
$template = fopen("../Header/Template.php", "r"); // with the appropriate checks of course
$data = str_replace( '/*-headerbanner-*/', $banner, $template);
//etc, str_replace your vars
$handle = fopen("../Header/" . $name . ".php", 'w'); // with the appropriate checks of course
fwrite($handle, $data); // with the appropriate checks of course
print "Data Written"; 
fclose($handle); 
?>

Link to comment
Share on other sites

try this:

 

<?php
$banner = "/images/" . $name . "/Banner.png"; 
$template = file_get_contents('../Header/Template.php');
$data = str_replace( '/*-headerbanner-*/', $banner, $template);
//etc, str_replace your vars
file_put_contents('../Header/'. $name. '.php', $data);
print "Data Written"; 
?>

 

but if the folder structure is always

"/images/" . $name . "/Banner.png";

 

i would put it in the template as

"/images//*-headerbanner-*//Banner.png"

 

and then:

<?php
$template = file_get_contents('../Header/Template.php');
$data = str_replace( '/*-headerbanner-*/', $name, $template);
//etc, str_replace your vars
file_put_contents('../Header/'. $name. '.php', $data);
print "Data Written"; 
?>

Link to comment
Share on other sites

ive made a website where the user might want to add a branch. the website has a login and each user has a branch id associated with it. when they log in it loads a specific css file and header file. what ive done is when the admin user wants to add a new brnch i didnt want a phone call saying could you add it in so ive made a page where they enter the branch name and select a colour then upload a banner and the page created everything like the header file the css file and the images for that branch. it means little interaction from the user and little chance for me to miss something.

 

 

Link to comment
Share on other sites

Your missing the entire point of a 'dynamic website'. Do you think we need to copy files around every time we add a new board to these forums? Better still, do you think a new page is created every time someone asks a new question? No.

 

Store your data in a database and you can use the same page over and over.

Link to comment
Share on other sites

im working on that atm but for a quick setup i needed to do this as multiple branches and branch setup wernt asked of originally. my plan for the future is to use a single header file. this was the only copy needed in the future is the css file as each branch will have different colours.

 

thank you for the advice though it is much appreciated

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.