Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10713

Re: Apache + php + directory browsing + reader for TXT files

From Salvatore <sal@yojimbo.hack.invalid>
Newsgroups comp.lang.php
Subject Re: Apache + php + directory browsing + reader for TXT files
Date 2013-09-04 14:56 +0000
Organization Yojimbo NNTP server
Message-ID <l07hm2$1s7g$1@news.tokachu.com> (permalink)
References <52272b3b$0$2211$426a74cc@news.free.fr>

Show all headers | View raw


On 2013-09-04, G. <grumsk@grumsk.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++

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Apache + php + directory browsing + reader for TXT files "G." <grumsk@grumsk.tz> - 2013-09-04 12:44 +0000
  Re: Apache + php + directory browsing + reader for TXT files Salvatore <sal@yojimbo.hack.invalid> - 2013-09-04 14:56 +0000
    Re: Apache + php + directory browsing + reader for TXT files The Natural Philosopher <tnp@invalid.invalid> - 2013-09-04 16:17 +0100
    Re: Apache + php + directory browsing + reader for TXT files Luuk <luuk@invalid.lan> - 2013-09-06 19:22 +0200

csiph-web