Jump to content

Recommended Posts

// Download file
$filename="whatever.txt"; //or address as well such as C:/files/whatever.txt
$size=filesize($filename);
header("Pragma: public");
header("Cache-Control: private");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=whatever.txt");
header("Content-length: $size");
readfile("$filename"); 

// Download file
$filename="whatever.txt"; //or address as well such as C:/files/whatever.txt
$size=filesize($filename);
header("Pragma: public");
header("Cache-Control: private");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=whatever.txt");
header("Content-length: $size");
readfile("$filename"); 

 

Sorry men, I have no idea how your code works. I tried copy pasting them all in a blank php file, and it only showed Warnings.

write a function

 


function downloadFile( $file)
{
$filename=$file;
$size=filesize($filename);
header("Pragma: public");
header("Cache-Control: private");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-length: $size");
readfile("$filename"); 
}

 

then pass it the file name and location of what you want to download

 

downloadFile( "c:/file/whatever.txt");

 

Woundln't it just be easier to make a .htaccess file and allow directory browsing for the download dir?

 

Ermm.... i just dont know how that works. XD  :D

 

And what im trying to do is to have a download dir, with many user dir to group users' files in a certain directory.

Open up a text editor. If windows use Wordpad...

 

Save the file right off as .htaccess into the dir that is the download dir.  Or someplace on your computer if you need to upload it via FTP...

 

Now inside the .htaccess put this.

 

Options +Indexes

 

 

Save.

 

Upload if needed.. Make sure it is only in the dir that you want users to browse... Then viola your set...  To make things a bit more safe make another .htaccess and this time put it in the parent directory ".."

 

Options -Indexes

 

This way if anyone clicks Parent Directory they won't get to see anything like the download dir.. They will either get a 404 error or the index.php,html,htm will load.

Open up a text editor. If windows use Wordpad...

 

Save the file right off as .htaccess into the dir that is the download dir.  Or someplace on your computer if you need to upload it via FTP...

 

Now inside the .htaccess put this.

 

Options +Indexes

 

 

Save.

 

Upload if needed.. Make sure it is only in the dir that you want users to browse... Then viola your set...  To make things a bit more safe make another .htaccess and this time put it in the parent directory ".."

 

Options -Indexes

 

This way if anyone clicks Parent Directory they won't get to see anything like the download dir.. They will either get a 404 error or the index.php,html,htm will load.

 

What youre suggesting is an FTP-like kind of download? Am I right?

Both your suggestions were helpful, but its not what i need. Paul's codes were good in terms of downloading. However, what i want is to view these files inside a download directory, not using FTP.  ;D Because a user can only download his created files.  ;D

No no no its not using FTP..

 

It shows you the DIR thru your browser...  I thought you ment "Like" ftp.. But it is no way FTP..

 

The way I say is if you make a link to your DIR with the .htaccess file in it.. It will just show them that directory as long as no htm, asp, php, html, shtml - index page is it in.. 

 

It just show you the dir as is with files and sub directories if they exist. Click a file in it and it will download.

 

Just try it and if you don't like it delete the .htaccess file and you are back to square one.

No no no its not using FTP..

 

It shows you the DIR thru your browser...  I thought you ment "Like" ftp.. But it is no way FTP..

 

The way I say is if you make a link to your DIR with the .htaccess file in it.. It will just show them that directory as long as no htm, asp, php, html, shtml - index page is it in.. 

 

It just show you the dir as is with files and sub directories if they exist. Click a file in it and it will download.

 

Just try it and if you don't like it delete the .htaccess file and you are back to square one.

 

Yep, i get the idea of it. But its not really the thing i want. FTP-like, correct me for that.  ;D

 

So anyone has any idea how to show files from a specific folder? By show, it means it is seen in a browser.

Here, I found something for directory listing and modified it for you:

<?php

function dirList($directory) 
{

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}


$mydir = "yourdirectory";
$list = dirList($mydir);

foreach($list as $item) {

echo '<a href="download.php?file='.$item.'">'.$item.'</a><br>';

}

?>


On the receiving page have something like what paul showed:

function downloadFile($file)
{
$filename=$file;
$size=filesize($filename);
header("Pragma: public");
header("Cache-Control: private");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-length: $size");
readfile("$filename"); 
}


then pass it the file name and location of what you want to download

downloadFile($_GET['file']);

 

Keep in mind you may need to modify this and the GET variables being passed to find the correct directory  as well as for verifying login or whatever else you want to add.

Another Simple one:

 

<?php
$path = "./"; // path to the directory to read ( ./ reads the dir this file is in)
if ($handle = opendir($path)) {
   while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        if(!is_dir($file)){
            $item[] = $file;
            }
       }
   }
   closedir($handle);
}

$total_items = count($item);
$max_items = ceil($total_items / 3); // items per <td>
$start = 0;
$end = $max_items
//generate the table
?>
<table width="100%" border="1" cellspacing="0" cellpadding="2">
  <tr>
    <?php
    for($i=0; $i<3; $i++){
        if($i != 0){
            $start = $start + $max_items;
            $end   = $end + $max_items;
        }
        echo "<td>";
        for($x = $start; $x < $end; $x++){
            // display the item
            echo '<a href = "'.$path.$item[$x] .'">';
            echo $item[$x]."</a><br>";
        }
    echo "</td>";
    }
    ?>
  </tr>
</table>

If you want to add a little MD5 to it I added this so that it would checksum each file with a MD5 Hash to verify that its what they got from you.

 

<?php
$path = "./"; // path to the directory to read ( ./ reads the dir this file is in)
if ($handle = opendir($path)) {
   while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        if(!is_dir($file)){
            $item[] = $file;
            $md5[] = md5_file($file);
            $md5tag[] = "MD5 Checksum: ";
            }
       }
   }
   closedir($handle);
}

$total_items = count($item);
$max_items = ceil($total_items / 3); // items per <td>
$start = 0;
$end = $max_items
//generate the table
?>
<table width="100%" border="1" cellspacing="0" cellpadding="2">
  <tr>
    <?php
    for($i=0; $i<3; $i++){
        if($i != 0){
            $start = $start + $max_items;
            $end   = $end + $max_items;
        }
        echo "<td>";
        for($x = $start; $x < $end; $x++){
            // display the item
            echo '<a href = "'.$path.$item[$x] .'">';
            echo $item[$x]."</a><br>";
            echo $md5tag[$x].$md5[$x]."<br>";
        }
    echo "</td>";
    }
    ?>
  </tr>
</table>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.