Jump to content


Photo

Listing and Downloading Files


  • Please log in to reply
5 replies to this topic

#1 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 08 June 2006 - 01:58 PM

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
Shishya

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 08 June 2006 - 02:43 PM

<?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);
   }
}
?>


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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 08 June 2006 - 03:37 PM

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 !

<? 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);
   }
}
?>

Shishya

#4 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 08 June 2006 - 05:30 PM


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
Shishya

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 June 2006 - 05:57 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 swatisonee

swatisonee
  • Members
  • PipPipPip
  • Advanced Member
  • 253 posts

Posted 08 June 2006 - 06:09 PM

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.


<?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>

Shishya




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users