Automatically create pages from files in directory

Thread Solved

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

Automatically create pages from files in directory

 
0
  #1
Oct 23rd, 2009
Hey guys,

Hopefully someone out there can help me.

I've written a script that displays the contents of a directory. It also links to the file so when I click on it it displays the file contents.

What I want to do is when I click on that file it creates that a new page with a header and footer include with the contents in the middle.

So far this is the code I have:

  1. <?php
  2.  
  3. for ($j=65; $j<=90; $j++){
  4. echo '<div class="list">';
  5. $a=chr($j);
  6. if ($a != "J" && $a != "Q" && $a != "X" && $a != "Z") {
  7. echo '<div class="large">'.$a.'</div>';
  8. echo '<div class="small">';
  9. if ($handle = opendir('poems/')) {
  10. while (false !== ($file = readdir($handle))) {
  11. if ($file != "." && $file != ".." && $file != "Q" && $file != "J" && $file != "X" && $file != "Z" && $file[0] == $a) {
  12. echo '<div class="test">';
  13. echo "<a href=\"poems/$file\" class='nav'>$file</a><br />";
  14. echo '</div>';
  15. }
  16. }
  17. }
  18. echo '</div>';
  19. closedir($handle);
  20. }
  21. echo '</div>';
  22. }
  23. ?>

Any help would be amazing! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,097
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 138
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
-1
  #2
Oct 23rd, 2009
In your original code - just make this small change:

  1. ........(rest of code)...........
  2. if ($file != "." && $file != ".." && $file != "Q" && $file != "J" && $file != "X" && $file != "Z" && $file[0] == $a) {
  3. //urlencode the filename and call it $uFile...
  4. $uFile = urlencode($file);
  5. //make a salted hash of the $uFile - change mysaltstring to something else
  6. $conf = md5('mysaltstring' . $uFile);
  7. echo '<div class="test">';
  8. echo "<a href=\"poems/fileviewer.php?f=$uFile&c=$conf\" class='nav'>$file</a><br />";
  9. echo '</div>';
  10. }
  11. }
  12. }
  13. echo '</div>';
  14. closedir($handle);
  15. }
  16. echo '</div>';
  17. }

Just have a php page called fileviewer.php

  1. <?php
  2. //check that the confirm hash is valid for the filename - a simple check against somebody messing with the querystring
  3. if(isset($_GET['c']) && isset($_GET['f']) && $_GET['c'] == md5('mysaltstring' . $_GET['f']) && file_exists(urldecode($_GET['f']))){
  4. //the checks are passed
  5. $file = urldecode($_GET['f']); //you could also 'sanitize' this step
  6. $output = file_get_contents($file);
  7. }else{
  8. $output = "<p>No such file found</p>\n<p><a href="findex.php">Return to file index</a></p>";
  9. }
  10. ?>
  11.  
  12. ...DTD...
  13. ...head...
  14. <body>
  15. ...your header html...
  16. <div id="fileoutput">
  17. <?php
  18. echo $output;
  19. ?>
  20. </div>
  21. ...your footer html...
  22. </body>
  23. </html>

Note - this is off the top of my head and is not tested. It should work, but if not - it's not far off.

BTW: A nicer way to do this would be to implement ajax to reload a div element in a page with a list of available files alongside.

Another thing you could do is send the link to the same page so that the links show up with the content.

There are quite a few ways you could go with this.
Last edited by ardav; Oct 23rd, 2009 at 7:27 pm.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 20
Reputation: peck3277 is an unknown quantity at this point 
Solved Threads: 0
peck3277 peck3277 is offline Offline
Newbie Poster
 
0
  #3
Oct 23rd, 2009
I might look into using ajax alright. I haven't used it before but I have read a lot of good things about it.

The code you gave me works brilliantly I just have 2 little problems that I don't know how to correct.

The first one is that some of the files im using have spaces in them. the code works for single named files, but not files with spaces in them. Is there any way to get past this?

And the second is some of the file names have special characters like í. Is there a way I can get around this?

If I could achieve the above it would be great but if not I could rename the files but when displaying the names I would display the first line of the file as this has also got the title. I would prefer to do the above but if I couldn't how would i go about doing things this way?

Thanks a million man!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,097
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 138
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster
 
0
  #4
Oct 24th, 2009
The spaces and special characters are a problem, and they tend to cause different problems if on a windows or linux server. If these are your files, simply rename them. If filenames are supplied by users, perhaps you could can a validation routine to "disallow" spaces and accented characters. Force all filenames to be alphanumeric.

You could use a renaming function to replace all spaces with an underscore.

However, this topic was discussed recently here:

http://www.daniweb.com/forums/thread232363.html

and I believe the thread-starter found a solution

//EDIT

Sorry, just re-read your post. With regard to title. Am I correct in assuming the filename is the title? If so and you use underscores in filenames (instead of spaces) - just use:

  1. $rawtitle = replace_str('_',' ',$file);
  2. //If you don't need the file extension for the title:
  3. $title = substr($rawtitle,0, strpos($rawtitle,'.'));
  4. //I can't remember if the third parameter needs to be +1, -1 or if it's ok as is - always gets me that one!

The $title can be forced to uppercase or lowercase with strtoupper() or strtolower() respectively.

Alternatively, you could use CSS to style the title:

  1. h3.filetitle {text-transform:uppercase}
  2. /*OR*/
  3. h3.filetitle {text-transform:capitalize}
  4. /*OR*/
  5. h3.filetitle {text-transform:lowercase}
The 'capitalize' will give "A Title With Capitals For All Words"
But you probably knew that.
Last edited by ardav; Oct 24th, 2009 at 6:02 am.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 20
Reputation: peck3277 is an unknown quantity at this point 
Solved Threads: 0
peck3277 peck3277 is offline Offline
Newbie Poster
 
0
  #5
Oct 24th, 2009
Yes you were right about the file name being the title. Thanks for all the help. Everything works as it should now!

What I eneded up doing was renaming my files with no spaces or special characters and because there are title was also the first line of of the file I just put that in an array and then displayed that instead of the file name itself!

Thanks again!
Reply With Quote Quick reply to this message  
Reply

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




Views: 335 | Replies: 4
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC