Help with displaying text files

Thread Solved

Join Date: Jul 2009
Posts: 16
Reputation: peck3277 is an unknown quantity at this point 
Solved Threads: 0
peck3277 peck3277 is offline Offline
Newbie Poster

Help with displaying text files

 
0
  #1
Sep 17th, 2009
Hi guys,

So today I am trying to do something that is probably quite simple. But my head is all over the place and I can barely think straight! Something which is not good for a newb!

I'm trying to display some text files on the web. These text files are all in their own seperate directory. I want to be able to list the directory. When you the file you want to view you are brought to a page which uses this code to display the text:

  1. <?php
  2. $myFile = "directory/file.txt";
  3. $fh = fopen($myFile, 'r');
  4. $theData = fread($fh, filesize($myFile));
  5. fclose($fh);
  6. echo "<pre>$theData</pre>";
  7. ?>

The code I have to display the directory is:

  1. <?php
  2. $dir = "poems/";
  3. if($scan = scandir($dir)){
  4. foreach ($scan as $value){
  5. if($value != "." && $value != ".."){
  6. echo $value."<br />";
  7. }
  8. }
  9. }
  10. ?>

So in summary what I want is:
1. Using the second piece of code or similar display a list of contents.
2. Once you choose a link the page it displays using the first piece of code or similar.

I presume I should be using some kind of template solution to make my site run the way i want.

I'm sorry if I explained it all over the place but as I said im kind of all over the place at the moment! Thanks guys
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 2
dasatti dasatti is offline Offline
Light Poster

Re: Help with displaying text files

 
0
  #2
Sep 17th, 2009
Try this code. I have written but not tested it, hopefully it will work for you.

  1. <?php
  2.  
  3.  
  4. $dir="poems/";
  5. printContents($dir);
  6.  
  7. if(isset($_REQUEST['f']))
  8. {
  9. $fh = fopen($_REQUEST['g'],"r");
  10. $theData = fread($fh,filesize($_REQUEST['f']));
  11. fclose($fh);
  12. echo "<pre>$theData</pre>";
  13. }
  14.  
  15. function printContents($directory)
  16. {
  17. if($scan = scandir($directory))
  18. {
  19. foreach ($scan as $value)
  20. {
  21. if (is_dir($value) && $value!="." && $value!="..") {
  22. printContents($directory);
  23. }
  24. else if(is_file($value))
  25. {
  26. echo "<br/>Read contents for ";
  27. echo "<a href=\"content_page.php?f=".$directory."/".$value."\">";
  28. echo $directory."/".$value."</a>";
  29. }
  30. }
  31. }
  32. }
  33. ?>
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: peck3277 is an unknown quantity at this point 
Solved Threads: 0
peck3277 peck3277 is offline Offline
Newbie Poster

Re: Help with displaying text files

 
0
  #3
Sep 18th, 2009
Hi Dasatti!

Thanks you very much for your help! Unfortunately the code isn't outputting anything. Can you please help shed some light on this? Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 2
dasatti dasatti is offline Offline
Light Poster

Re: Help with displaying text files

 
0
  #4
Sep 18th, 2009
Please try it now

  1. <?php
  2.  
  3.  
  4. $dir="poems/";
  5. printContents($dir);
  6.  
  7. if(isset($_REQUEST['f']))
  8. {
  9. $fh = fopen($_REQUEST['f'],"r");
  10. $theData = fread($fh,filesize($_REQUEST['f']));
  11. fclose($fh);
  12. echo "<pre>$theData</pre>";
  13. }
  14.  
  15. function printContents($directory)
  16. {
  17.  
  18. if($scan = scandir($directory))
  19. {
  20. foreach ($scan as $value)
  21. {
  22. if (is_dir($value) && $value!="." && $value!="..") {
  23. printContents($directory);
  24. }
  25. else
  26. {
  27. echo "<br/>Read contents for ";
  28. echo "<a href=\"content_page.php?f=".$directory."/".$value."\">";
  29. echo $directory."/".$value."</a>";
  30. }
  31. }
  32. }
  33. }
  34. ?>
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: peck3277 is an unknown quantity at this point 
Solved Threads: 0
peck3277 peck3277 is offline Offline
Newbie Poster

Re: Help with displaying text files

 
0
  #5
Sep 18th, 2009
Dasatti thanks once again for you help. The code for outputting the directory list is working perfectly.

However there are 2 small problems. One it's outputting the dots at the top:

.
..

And the second when I click the link I get the following:

The requested URL /Site/content_page.php was not found on this server.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: Help with displaying text files

 
0
  #6
Sep 18th, 2009
Hi there,

Here's the modified version of Dasatti's printContents:

  1. <?php
  2.  
  3. $dir = "poems/";
  4. printContents($dir);
  5.  
  6. if(isset($_REQUEST['f'])){
  7. $fh = fopen($_REQUEST['f'],"r");
  8. $theData = fread($fh,filesize($_REQUEST['f']));
  9. fclose($fh);
  10. echo "<pre>$theData</pre>";
  11. }
  12.  
  13. function printContents($dir) {
  14. $mydir = opendir($dir);
  15. while(false !== ($file = readdir($mydir))) {
  16. if($file != "." && $file != "..") {
  17. if(is_dir($dir.$file)) {
  18. printContents($dir.$file) ;
  19. }
  20. else{
  21. echo "<br/>Read contents for ";
  22. echo "<a href=\"list.php?f=".$dir."/".$file."\">";
  23. echo $dir.$file."</a>";
  24. }
  25. }
  26. }
  27. closedir($mydir);
  28. }
  29.  
  30. ?>

You probably forgot to change the target page content_page.php to the one which you are testing your code in. In my case, i was using a file code list.php, hence in the anchor tag i used list.php.
Last edited by wilch; Sep 18th, 2009 at 4:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: dasatti is an unknown quantity at this point 
Solved Threads: 2
dasatti dasatti is offline Offline
Light Poster

Re: Help with displaying text files

 
0
  #7
Sep 19th, 2009
Either rename the page to content_page.php or change the href attribute in line no 28 to the name of the file to which you saved it with.

  1. echo "<a href=\"<place_the_name_of_the_php_file_here>?f=".$directory."/".$value."\">";
maSteR of aLL, jAck oF NoNe
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 16
Reputation: peck3277 is an unknown quantity at this point 
Solved Threads: 0
peck3277 peck3277 is offline Offline
Newbie Poster

Re: Help with displaying text files

 
0
  #8
Oct 1st, 2009
Sorry I took so long in getting back guys, I was away for a bit there.

Anyways the code works beautifully so thank ye so much for the help!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: Help with displaying text files

 
0
  #9
Oct 2nd, 2009
You're welome.
umm.. by the way how do you do it ?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC