Jump to content

Archived

This topic is now archived and is closed to further replies.

swatisonee

Listing and Downloading Files

Recommended Posts

Hello,


My users upload some files onto a password protected folder with 777 permissions on my server.

Now I need to create a PHP file that determines the files in that folder and then creates an index.html file that lists whatever files are in that folder as hyperlinks to enable them being downloaded.

Is there a way this can be done? I am very unsure of working with classes and functions and would prefer not to use them unless there is some readily available script that i can copy-paste.

Any suggestions pl ?

Thansks:Swati

Share this post


Link to post
Share on other sites
[code]<?php
$dir = "where/the/files/are/kept/";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo 'File: <a href="'.$dir.$file.'">'.$file.'</a><Br>";
       }
       closedir($dh);
   }
}
?>[/code]


I think that should do the job for you :smiley: Simple just add that to the page u want the files to be listed on and then when they click the link if they havn't already authenticated it will just ask as normal :)


Regards
Liam

Share this post


Link to post
Share on other sites
Hi,

Ummm..i think there's some syntax error but i am not able to locate it - its resulting in a blank page. Can you pl check ? Thanks !

[code]<? php

$dir = "/support/machine/data/";

if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo 'File: <a href="'.$dir.$file.'">'.$file.'  </a><Br>";
// the RHS double quote is unsupported but when i replaced it with a single quote i still got a blank page
       }
       closedir($dh);
   }
}
?>[/code]

Share this post


Link to post
Share on other sites

Hi,

[a href=\"http://www.phpfreaks.com/quickcode/Reading-in-Files-from-a-Directory-and-Editing-Them/68.php\" target=\"_blank\"]http://www.phpfreaks.com/quickcode/Reading...ing-Them/68.php[/a].

I dont know if the author will read this but the link is almost what i am looking for **But**

the page looks like this.


TodaysSituation.doc | EDIT
SME.ppt | EDIT
.htaccess.bak
.htaccess
1499.jpg



Can someone pl tell me how to remove the .htaccess, .htbak stuff and if possible to put the data in 4 columns : S. No, File Name, Date Modified , Size

Thanks in advance

Swati

Share this post


Link to post
Share on other sites
you are going to have to make a condition to check for those filenames and not print it if that's the file in the loop. best way would be to make an array of the filenames that you do not wish to be echoed, and inside the loop, run a foreach loop to check the current filename against each element in the array.

as far as displaying it all pretty like that... just format it with some html tables.

Share this post


Link to post
Share on other sites
Hi,

Umm ok i will work on that but in the meantime, some more creative googling found me the following code. I'd like to be able to combine both the codes as it would then give me the result i'm looking for.

I'm finding that i need to insert this code in between parts of


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--][a href=\"http://www.phpfreaks.com/quickcode/Reading...ing-Them/68.php\" target=\"_blank\"]http://www.phpfreaks.com/quickcode/Reading...ing-Them/68.php[/a]. [/quote]


However :


a) the directory where the files are located are defined separately in each code and i'm getting caught in a maze.

b) i will still need to find a way to avoid the htaccess from listing.

c) Although my directory is password protected, using the above code i find that the pp is being bypassed and the user can directly view/edit the file . This is a huge problem that i am clueless to resolve

Any pointers pleasE ?Thanks.


[code]<?php
            // define directory to list
            // (can be given by REQUEST, too)

            //$str_dir = "yourDirectory/";

            if(!isset($_REQUEST['dir']) && !isset($str_dir)){
                $str_dir = "./";
            }else{
                $str_dir = $_REQUEST['dir'];
            }

            // open directory
            $dir = opendir($str_dir);

            // create array (for sorting before output)
            $files = array();

            // sort by...
            if(!isset($_REQUEST['sortby'])){
                $sortby = "name";
            }else{
                $sortby = $_REQUEST['sortby'];
            }
            switch($_REQUEST['sortmode'])
            {
                case "DESC":$sortmode = SORT_DESC;break;
                case "ASC":
                default:$sortmode = SORT_ASC;
            }

            // read files
            $test[$i]= 0;
            while($file = readdir($dir))
            {
                if(($file != "..") && ($file != ".") && !is_dir($file))
                {
                    $files[$i] = array(
                        "name"=>$file,
                        "size"=>filesize($file),
                        "ctime"=>filectime($file),
                        "mtime"=>filemtime($file),
                        );

                    // doubled, to be able to sort an
                    // multidimensional array well
                    $files[$i]["0"] = $files[$i][$sortby];
                    ksort($files[$i]);

                    $i++;
                }
            }
            closedir($dir);

            // sort
            array_multisort($files, $sortmode);

            // output
            echo "<table>";
            echo "<tr>";
            echo "<th>filename<br>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=name&sortmode=ASC\">(asc)</a>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=name&sortmode=DESC\">(desc)</a>";
            echo "</th>\n";
            echo "<th>size<br>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=size&sortmode=ASC\">(asc)</a>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=size&sortmode=DESC\">(desc)</a>";
            echo "</th>\n";
            echo "<th>date created*<br>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=ctime&sortmode=ASC\">(asc)</a>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=ctime&sortmode=DESC\">(desc)</a>";
            echo "</th>\n";
            echo "<th>last modified<br>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=mtime&sortmode=ASC\">(asc)</a>";
                echo "<a href=\"".$_SERVER['PHP_SELF']."?sortby=mtime&sortmode=DESC\">(desc)</a>";
            echo "</th>\n";
            echo "</tr>";
            for($i = 0; $i < count($files); $i++)
            {
                echo "<tr>";
                echo "<td width=\"200\"><a href=\"" . $str_dir . $files[$i]['name'] . "\">" . $files[$i]['name'] . "</a></td>\n";
                echo "<td>" . $files[$i]['size'] . " Bytes</td>\n";
                echo "<td>" . date("d.m.Y G:i", $files[$i]['ctime']) . "</td>\n";
                echo "<td>" . date("d.m.Y G:i", $files[$i]['mtime']) . "</td>\n";
                echo "</tr>\n";
            }
        ?>
        <tr><td colspan="4">
       <small>*  ************************************************.</small>

        </td></tr>
        </table>
    </body>
</html>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.