How Do I Include a text file with php

  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

i want all the content of my site to be in text documents. i want to use a php include on my index page where it will load the correct text file by which link is clicked. im not real sure how this is done. its been years since ive done any designing and my memory is pretty rusty. i want the links to look like this. index.php?=content. im sure someone here can help me out
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

ok still no replies. let me make this a little more clear. i know there are some very skilled webmasters that use this site daily. i want to create an index page that will be used as the layout and define everything other then the content that is displayed. i want to write all the content in seperate text files but have the include function call upon the proper text file by which link is clicked. therefore the links would look similiar to this index.php?=textfilename
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Assuming the URL http://www.domain.com/?page=mypage

The following code will look for a file named "mypage.txt" in a directory relative to the script named "pages".

PHP Code: [ Select ]
<?php
 
if( ! empty($_GET['page']) && ! (bool)strcspn($_GET['page'], 'abcdefghijklmnopqrstuvwxyz') && file_exists("./pages/{$_GET['page']}.txt"))
{
   include("./pages/{$_GET['page']}.txt");
}
 
?>
  1. <?php
  2.  
  3. if( ! empty($_GET['page']) && ! (bool)strcspn($_GET['page'], 'abcdefghijklmnopqrstuvwxyz') && file_exists("./pages/{$_GET['page']}.txt"))
  4. {
  5.    include("./pages/{$_GET['page']}.txt");
  6. }
  7.  
  8. ?>


The strcspn call in there will bail on any value sent via the page get variable that contains anything other than lowercase a-z
  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

thanks for the reply joebert. now do i have to put this script in its own php file or can i include it where i want the text file to load in the index.php. i have a layout designed but i dont want to include the layout on every page. therefore the content needs to be in seperate files that are loaded depending on what link is clicked where the address would be index.php?page=mypage. is this what the above script does?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Assuming your index.php has a combination of HTML and PHP in it already, you would put the above code at the place in that index.php file where you want the content of the text file to be displayed.

PHP Code: [ Select ]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <meta http-equiv="content-type" content="text/html;charset=utf-8" />
   <title>My Site</title>
</head>
<body>
 
<div id="content">
<?php
   if( ! empty($_GET['page']) && ! (bool)strcspn($_GET['page'], 'abcdefghijklmnopqrstuvwxyz') && file_exists("./pages/{$_GET['page']}.txt"))
   {
      include("./pages/{$_GET['page']}.txt");
   }
?>
</div>
 
<div id="sidebar"> ... </div>
 
</body>
</html>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3. <head>
  4.    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  5.    <title>My Site</title>
  6. </head>
  7. <body>
  8.  
  9. <div id="content">
  10. <?php
  11.    if( ! empty($_GET['page']) && ! (bool)strcspn($_GET['page'], 'abcdefghijklmnopqrstuvwxyz') && file_exists("./pages/{$_GET['page']}.txt"))
  12.    {
  13.       include("./pages/{$_GET['page']}.txt");
  14.    }
  15. ?>
  16. </div>
  17.  
  18. <div id="sidebar"> ... </div>
  19.  
  20. </body>
  21. </html>


There would be a directory named "pages" along-side your index.php file and that directory would be filled with the txt files.

Attachments:
folder.jpg


Here's an example attached, there is an htaccess file inside the examples pages folder that prevents people from accessing any of the txt files directly from their browser.
Attachments:
example.zip

(1.18 KiB) Downloaded 308 times

  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

thanks joebert. ill try this. looks to be exactly what im needing.
  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

what you showed me works great. however, how would i have the php load up a page by default when the site is visited like the main page. where it would automatically display home.txt
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

After the IF you could use an ELSE.

PHP Code: [ Select ]
else
{
    include("./pages/home.txt");
}
  1. else
  2. {
  3.     include("./pages/home.txt");
  4. }
  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

the problem im having now is that the background wont repeat so the content just streams right past it. im not real sure how i can get the background to repeat indefinately. maybe you can help me out joebert. to see what im talking about you can go here:

http://www.blockworkmusic.com/index.php?page=services
  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

i corrected the DOCTYPE and it fixed the problem for opera but IE is still showing a gap in the background. someone help me out! heres the code for my index.php file.

Code: [ Select ]
<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 ">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Blockwork Music Recording Studio</title>
<style type="text/css">
body,td,th {
    color: #FFF;
}
body {
    background-image: url(Images/bg1.jpg);
    background-repeat: repeat-y;
    background-position: center;
    margin-top: 0px;
    padding-top: 0px;
    width: 100%;
    height:100%;
    position:absolute;
    
}

html {
    background-image:url(Images/bg.jpg);
    background-repeat: inherit;
    margin-bottom:0px;
    padding-bottom:0px;
    width:100%;
    height:100%;
}

.center {
  text-align: center;
    margin-top: 0px;

}
.bottom {
    background-image:url(Images/bg1.jpg);
    background-position: center;
    background-repeat:repeat-y;
  position:fixed;
  bottom: 0;
    margin-bottom:1px;
    padding-bottom:0;
    width: 100%;
    height: 20px;
    text-align:center;
}
</style>
</head>

<body link="#c2edfd" alink="#c2edfd" vlink="#c2edfd">
<p class="center"><img src="Images/header.jpg" width="1011" height="298" border="0" usemap="#Map"/>
 <map name="Map" id="Map">
  <area shape="rect" coords="102,267,188,297" href="index.php?page=home" />
  <area shape="rect" coords="227,269,366,306" href="index.php?page=services" />
  <area shape="rect" coords="406,266,540,299" href="index.php?page=contact" />
  <area shape="rect" coords="575,266,717,301" href="index.php?page=portfolio" />
  <area shape="rect" coords="749,268,840,300" href="index.php?page=links" />
 </map>
</p>
<table width="900" border="0" cellspacing="0" cellpadding="0" align="center">
 <tr>
  <td><p class="center"><img src="Images/banner.jpg" /></p><br /><?php

if( ! empty($_GET['page']) && ! (bool)strcspn($_GET['page'], 'abcdefghijklmnopqrstuvwxyz') && file_exists("./content/{$_GET['page']}.txt"))
{
  include("./content/{$_GET['page']}.txt");
}

else
{
  include("./content/home.txt");
}

?></td>
 </tr>
</table></p><br />
<p class="bottom"><font size="2">&copy 2010 Blockwork Music. All Rights Reserved.</font></p>
</body>
</html>
  1. <!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 ">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Blockwork Music Recording Studio</title>
  6. <style type="text/css">
  7. body,td,th {
  8.     color: #FFF;
  9. }
  10. body {
  11.     background-image: url(Images/bg1.jpg);
  12.     background-repeat: repeat-y;
  13.     background-position: center;
  14.     margin-top: 0px;
  15.     padding-top: 0px;
  16.     width: 100%;
  17.     height:100%;
  18.     position:absolute;
  19.     
  20. }
  21. html {
  22.     background-image:url(Images/bg.jpg);
  23.     background-repeat: inherit;
  24.     margin-bottom:0px;
  25.     padding-bottom:0px;
  26.     width:100%;
  27.     height:100%;
  28. }
  29. .center {
  30.   text-align: center;
  31.     margin-top: 0px;
  32. }
  33. .bottom {
  34.     background-image:url(Images/bg1.jpg);
  35.     background-position: center;
  36.     background-repeat:repeat-y;
  37.   position:fixed;
  38.   bottom: 0;
  39.     margin-bottom:1px;
  40.     padding-bottom:0;
  41.     width: 100%;
  42.     height: 20px;
  43.     text-align:center;
  44. }
  45. </style>
  46. </head>
  47. <body link="#c2edfd" alink="#c2edfd" vlink="#c2edfd">
  48. <p class="center"><img src="Images/header.jpg" width="1011" height="298" border="0" usemap="#Map"/>
  49.  <map name="Map" id="Map">
  50.   <area shape="rect" coords="102,267,188,297" href="index.php?page=home" />
  51.   <area shape="rect" coords="227,269,366,306" href="index.php?page=services" />
  52.   <area shape="rect" coords="406,266,540,299" href="index.php?page=contact" />
  53.   <area shape="rect" coords="575,266,717,301" href="index.php?page=portfolio" />
  54.   <area shape="rect" coords="749,268,840,300" href="index.php?page=links" />
  55.  </map>
  56. </p>
  57. <table width="900" border="0" cellspacing="0" cellpadding="0" align="center">
  58.  <tr>
  59.   <td><p class="center"><img src="Images/banner.jpg" /></p><br /><?php
  60. if( ! empty($_GET['page']) && ! (bool)strcspn($_GET['page'], 'abcdefghijklmnopqrstuvwxyz') && file_exists("./content/{$_GET['page']}.txt"))
  61. {
  62.   include("./content/{$_GET['page']}.txt");
  63. }
  64. else
  65. {
  66.   include("./content/home.txt");
  67. }
  68. ?></td>
  69.  </tr>
  70. </table></p><br />
  71. <p class="bottom"><font size="2">&copy 2010 Blockwork Music. All Rights Reserved.</font></p>
  72. </body>
  73. </html>
  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

can anybody here help me out?
  • xlgfx
  • Graduate
  • Graduate
  • xlgfx
  • Posts: 232
  • Loc: saint joseph, missouri

Post 3+ Months Ago

for anybody else who ever has this problem its a simple fix. everything works great now in both opera and internet explorer.
i added this bit of code:
Code: [ Select ]
html>body {
min-height:100%;
height:auto;
}
  1. html>body {
  2. min-height:100%;
  3. height:auto;
  4. }

fixed the problem

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 70 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.