FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Apache + php + directory browsing + reader for TXT files
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Apache + php + directory browsing + reader for TXT files [message #182752] Wed, 04 September 2013 12:44 Go to next message
G. is currently offline  G.
Messages: 1
Registered: September 2013
Karma: 0
Junior Member
Hi, I have a bunch of *.txt files. I would like to put them on my server
(I already have a running apache2 with PHP5 and Mysql) and allow my students
to browse directories and read the TXT files.

I spent some time yesterday on applying a CSS to apache's "directory browsing"
ability.

Now, I would like to go a further step: applying a minimal style to my TXT
documents with a few rules. (font, size, margins, background color, color, etc.)
and dynamically producing links from my http://... strings.

I think it is possible to play with apache redirection settings and PHP
for dynamically build a *.txt reader. But maybe something has already be
done which could be a starting point for me.

Are you aware of that?

Two questions:
* (php related) what resources could I use for my purpose?
* (for apache wizzards) what would be the best way of handling my *.txt files
and redirecting to a small php "reader"?

Regards,

--
G.
Re: Apache + php + directory browsing + reader for TXT files [message #182753 is a reply to message #182752] Wed, 04 September 2013 14:56 Go to previous messageGo to next message
Salvatore is currently offline  Salvatore
Messages: 38
Registered: September 2012
Karma: 0
Member
On 2013-09-04, G. <grumsk(at)grumsk(dot)tz> wrote:
> Hi, I have a bunch of *.txt files. I would like to put them on my server
> (I already have a running apache2 with PHP5 and Mysql) and allow my students
> to browse directories and read the TXT files.
>
> I spent some time yesterday on applying a CSS to apache's "directory browsing"
> ability.
>
> Now, I would like to go a further step: applying a minimal style to my TXT
> documents with a few rules. (font, size, margins, background color, color, etc.)
> and dynamically producing links from my http://... strings.

I believe that this is as simple as creating a file called HEADER.html
(make sure you use uppercase lettersi for the word "HEADER") and include
some <style> tags.

>
> I think it is possible to play with apache redirection settings and PHP
> for dynamically build a *.txt reader. But maybe something has already be
> done which could be a starting point for me.
>
> Are you aware of that?

Yes. Find the option in your Apache configuration file called
"DirectoryIndex" and change it to include index.php. Here's an example:

DirectoryIndex index.html index.htm index.php

This will allow you to write your own version of a directory listing in
PHP. If you don't want to write your own directory listing script,
continue on below...

>
> Two questions:
> * (php related) what resources could I use for my purpose?

I would try using the mod_rewrite module.

> * (for apache wizzards) what would be the best way of handling my *.txt files
> and redirecting to a small php "reader"?

Here's what I came up with:

1. Create a file called "textfile.php". Put code in there that reads
the variable $_REQUEST['file'] and verifies that it doesn't
contain anything nasty. (Basically, verify that it only contains
the characters A-Z, a-z, 0-9, a dash, an underscore, no more than
one dot, and the extension ".txt".) Use that variable to open up a
text file.

2. Add a rewrite rule to the .htaccess file that looks something like
this:

RewriteEngine On
RewriteRule ^/textfiles/([^/]+\.txt)$ /textfile.php?file=$1 [PT]

(This presumes your text files are being hosted in the
subdirectory "/textfiles".)

3. In the file "textfile.php", use the function
"file_read_contents()" to read the contents of the text file.

And, of course, be extra careful when checking the file name so that
people cannot pass a parameter that looks like "file=../../../etc/passwd"
or something like that.

>
> Regards,
>


--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
Re: Apache + php + directory browsing + reader for TXT files [message #182754 is a reply to message #182753] Wed, 04 September 2013 15:17 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 04/09/13 15:56, Salvatore wrote:
> On 2013-09-04, G. <grumsk(at)grumsk(dot)tz> wrote:
>> Hi, I have a bunch of *.txt files. I would like to put them on my server
>> (I already have a running apache2 with PHP5 and Mysql) and allow my students
>> to browse directories and read the TXT files.
>>
>> I spent some time yesterday on applying a CSS to apache's "directory browsing"
>> ability.
>>
>> Now, I would like to go a further step: applying a minimal style to my TXT
>> documents with a few rules. (font, size, margins, background color, color, etc.)
>> and dynamically producing links from my http://... strings.
> I believe that this is as simple as creating a file called HEADER.html
> (make sure you use uppercase lettersi for the word "HEADER") and include
> some <style> tags.
>
>> I think it is possible to play with apache redirection settings and PHP
>> for dynamically build a *.txt reader. But maybe something has already be
>> done which could be a starting point for me.
>>
>> Are you aware of that?
> Yes. Find the option in your Apache configuration file called
> "DirectoryIndex" and change it to include index.php. Here's an example:
>
> DirectoryIndex index.html index.htm index.php
>
> This will allow you to write your own version of a directory listing in
> PHP. If you don't want to write your own directory listing script,
> continue on below...
>
>> Two questions:
>> * (php related) what resources could I use for my purpose?
> I would try using the mod_rewrite module.
>
>> * (for apache wizzards) what would be the best way of handling my *.txt files
>> and redirecting to a small php "reader"?
> Here's what I came up with:
>
> 1. Create a file called "textfile.php". Put code in there that reads
> the variable $_REQUEST['file'] and verifies that it doesn't
> contain anything nasty. (Basically, verify that it only contains
> the characters A-Z, a-z, 0-9, a dash, an underscore, no more than
> one dot, and the extension ".txt".) Use that variable to open up a
> text file.
>
> 2. Add a rewrite rule to the .htaccess file that looks something like
> this:
>
> RewriteEngine On
> RewriteRule ^/textfiles/([^/]+\.txt)$ /textfile.php?file=$1 [PT]
>
> (This presumes your text files are being hosted in the
> subdirectory "/textfiles".)
>
> 3. In the file "textfile.php", use the function
> "file_read_contents()" to read the contents of the text file.
>
> And, of course, be extra careful when checking the file name so that
> people cannot pass a parameter that looks like "file=../../../etc/passwd"
> or something like that.

by the time you have done that you might as well write a program called
'index.php' that looks for all the tect files in te deirectory and liste
them as hyperlinks to a 'read a text file and turn it into html' page.

if you do a file-get-contents and p[ass it through a couple of string
replacement passes to turn newlines into <BR> and htmlspecialchar to
replace control characters and wrap it in a header, it IS an HTML page.


>> Regards,
>>
>


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.
Re: Apache + php + directory browsing + reader for TXT files [message #182764 is a reply to message #182753] Fri, 06 September 2013 17:22 Go to previous message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 04-09-2013 16:56, Salvatore wrote:
> On 2013-09-04, G. <grumsk(at)grumsk(dot)tz> wrote:
>> Hi, I have a bunch of *.txt files. I would like to put them on my server
>> (I already have a running apache2 with PHP5 and Mysql) and allow my students
>> to browse directories and read the TXT files.
>>
>> I spent some time yesterday on applying a CSS to apache's "directory browsing"
>> ability.
>>
>> Now, I would like to go a further step: applying a minimal style to my TXT
>> documents with a few rules. (font, size, margins, background color, color, etc.)
>> and dynamically producing links from my http://... strings.
>
> I believe that this is as simple as creating a file called HEADER.html
> (make sure you use uppercase lettersi for the word "HEADER") and include
> some <style> tags.
>

see: http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html#headername

it does not need to be UPPERCASE, if i read the story on above link......
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: php exec ( ) DO NOT work for relative paths: Help?
Next Topic: [Mac OS X ML] how to install MongoDB driver
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 22:10:33 GMT 2024

Total time taken to generate the page: 0.02074 seconds