| | |
Help with displaying text files
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: 16
Reputation:
Solved Threads: 0
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:
The code I have to display the directory is:
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
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:
PHP Syntax (Toggle Plain Text)
<?php $myFile = "directory/file.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo "<pre>$theData</pre>"; ?>
The code I have to display the directory is:
PHP Syntax (Toggle Plain Text)
<?php $dir = "poems/"; if($scan = scandir($dir)){ foreach ($scan as $value){ if($value != "." && $value != ".."){ echo $value."<br />"; } } } ?>
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
•
•
Join Date: Dec 2008
Posts: 37
Reputation:
Solved Threads: 2
Try this code. I have written but not tested it, hopefully it will work for you.
php Syntax (Toggle Plain Text)
<?php $dir="poems/"; printContents($dir); if(isset($_REQUEST['f'])) { $fh = fopen($_REQUEST['g'],"r"); $theData = fread($fh,filesize($_REQUEST['f'])); fclose($fh); echo "<pre>$theData</pre>"; } function printContents($directory) { if($scan = scandir($directory)) { foreach ($scan as $value) { if (is_dir($value) && $value!="." && $value!="..") { printContents($directory); } else if(is_file($value)) { echo "<br/>Read contents for "; echo "<a href=\"content_page.php?f=".$directory."/".$value."\">"; echo $directory."/".$value."</a>"; } } } } ?>
maSteR of aLL, jAck oF NoNe
•
•
Join Date: Dec 2008
Posts: 37
Reputation:
Solved Threads: 2
Please try it now
php Syntax (Toggle Plain Text)
<?php $dir="poems/"; printContents($dir); if(isset($_REQUEST['f'])) { $fh = fopen($_REQUEST['f'],"r"); $theData = fread($fh,filesize($_REQUEST['f'])); fclose($fh); echo "<pre>$theData</pre>"; } function printContents($directory) { if($scan = scandir($directory)) { foreach ($scan as $value) { if (is_dir($value) && $value!="." && $value!="..") { printContents($directory); } else { echo "<br/>Read contents for "; echo "<a href=\"content_page.php?f=".$directory."/".$value."\">"; echo $directory."/".$value."</a>"; } } } } ?>
maSteR of aLL, jAck oF NoNe
•
•
Join Date: Jul 2009
Posts: 16
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
Hi there,
Here's the modified version of Dasatti's printContents:
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.
Here's the modified version of Dasatti's printContents:
PHP Syntax (Toggle Plain Text)
<?php $dir = "poems/"; printContents($dir); if(isset($_REQUEST['f'])){ $fh = fopen($_REQUEST['f'],"r"); $theData = fread($fh,filesize($_REQUEST['f'])); fclose($fh); echo "<pre>$theData</pre>"; } function printContents($dir) { $mydir = opendir($dir); while(false !== ($file = readdir($mydir))) { if($file != "." && $file != "..") { if(is_dir($dir.$file)) { printContents($dir.$file) ; } else{ echo "<br/>Read contents for "; echo "<a href=\"list.php?f=".$dir."/".$file."\">"; echo $dir.$file."</a>"; } } } closedir($mydir); } ?>
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.
•
•
Join Date: Dec 2008
Posts: 37
Reputation:
Solved Threads: 2
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.
php Syntax (Toggle Plain Text)
echo "<a href=\"<place_the_name_of_the_php_file_here>?f=".$directory."/".$value."\">";
maSteR of aLL, jAck oF NoNe
![]() |
Similar Threads
- reading from text files and manipulating the data....how (Visual Basic 4 / 5 / 6)
- manipulate text files (VB.NET)
- Trying to compare the contents of two text files and save the difference (Python)
- Need reference to Random Accessing of Text files (C++)
- Write and Read text files (Java)
- Making arrays from text files (VB.NET)
- appending two java text files (Java)
- lost my notebook for making text files--how do I get it back? (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: Help...! (uploading MySQl database on to sever)
- Next Thread: PHP Confirmation
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube





