| | |
Automatically create pages from files in directory
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 20
Reputation:
Solved Threads: 0
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:
Any help would be amazing! Thanks!
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)
<?php for ($j=65; $j<=90; $j++){ echo '<div class="list">'; $a=chr($j); if ($a != "J" && $a != "Q" && $a != "X" && $a != "Z") { echo '<div class="large">'.$a.'</div>'; echo '<div class="small">'; if ($handle = opendir('poems/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "Q" && $file != "J" && $file != "X" && $file != "Z" && $file[0] == $a) { echo '<div class="test">'; echo "<a href=\"poems/$file\" class='nav'>$file</a><br />"; echo '</div>'; } } } echo '</div>'; closedir($handle); } echo '</div>'; } ?>
Any help would be amazing! Thanks!
-1
#2 Oct 23rd, 2009
In your original code - just make this small change:
Just have a php page called fileviewer.php
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.
php Syntax (Toggle Plain Text)
........(rest of code)........... if ($file != "." && $file != ".." && $file != "Q" && $file != "J" && $file != "X" && $file != "Z" && $file[0] == $a) { //urlencode the filename and call it $uFile... $uFile = urlencode($file); //make a salted hash of the $uFile - change mysaltstring to something else $conf = md5('mysaltstring' . $uFile); echo '<div class="test">'; echo "<a href=\"poems/fileviewer.php?f=$uFile&c=$conf\" class='nav'>$file</a><br />"; echo '</div>'; } } } echo '</div>'; closedir($handle); } echo '</div>'; }
Just have a php page called fileviewer.php
php Syntax (Toggle Plain Text)
<?php //check that the confirm hash is valid for the filename - a simple check against somebody messing with the querystring if(isset($_GET['c']) && isset($_GET['f']) && $_GET['c'] == md5('mysaltstring' . $_GET['f']) && file_exists(urldecode($_GET['f']))){ //the checks are passed $file = urldecode($_GET['f']); //you could also 'sanitize' this step $output = file_get_contents($file); }else{ $output = "<p>No such file found</p>\n<p><a href="findex.php">Return to file index</a></p>"; } ?> ...DTD... ...head... <body> ...your header html... <div id="fileoutput"> <?php echo $output; ?> </div> ...your footer html... </body> </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!
•
•
Join Date: Jul 2009
Posts: 20
Reputation:
Solved Threads: 0
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!
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!
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:
The $title can be forced to uppercase or lowercase with strtoupper() or strtolower() respectively.
Alternatively, you could use CSS to style the title:
The 'capitalize' will give "A Title With Capitals For All Words"
But you probably knew that.
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)
$rawtitle = replace_str('_',' ',$file); //If you don't need the file extension for the title: $title = substr($rawtitle,0, strpos($rawtitle,'.')); //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)
h3.filetitle {text-transform:uppercase} /*OR*/ h3.filetitle {text-transform:capitalize} /*OR*/ h3.filetitle {text-transform:lowercase}
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!
•
•
Join Date: Jul 2009
Posts: 20
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- Listing files in a directory (C++)
- Perl script to organize files in directory structure (Perl)
- Import XML files from a directory to ACCESS (Visual Basic 4 / 5 / 6)
- read all files in a directory (C++)
- To open and close in turn all the files in a directory (C++)
- List files from a directory (C)
- code to use all .htm files in a directory (C)
- Help with Searching files in Directories (C++)
- Can't delete three .tmp files (Windows 95 / 98 / Me)
Other Threads in the PHP Forum
- Previous Thread: PHP or ASP.net
- Next Thread: why isnt this working?
Views: 335 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl customizableitems database date directory display download dynamic echo email error file files folder form format forms forum function functions google headmethod href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail malfunctioning menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






