944,184 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 903
  • PHP RSS
Oct 23rd, 2009
0

Automatically create pages from files in directory

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
peck3277 is offline Offline
60 posts
since Jul 2009
Oct 23rd, 2009
-1
Re: Automatically create pages from files in directory
In your original code - just make this small change:

php Syntax (Toggle Plain Text)
  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

php Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 954
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Oct 23rd, 2009
0
Re: Automatically create pages from files in directory
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
peck3277 is offline Offline
60 posts
since Jul 2009
Oct 24th, 2009
0
Re: Automatically create pages from files in directory
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:

php Syntax (Toggle Plain Text)
  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:

CSS Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 954
Disgraced Poster
ardav is offline Offline
6,728 posts
since Oct 2006
Oct 24th, 2009
0
Re: Automatically create pages from files in directory
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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
peck3277 is offline Offline
60 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP or ASP.net
Next Thread in PHP Forum Timeline: why isnt this working?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC