renj0806 Posted February 24, 2007 Share Posted February 24, 2007 Does anyone know where I can find a good tutorial or sample that involves downloading a file from the web server? Using ofcourse PHP. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/ Share on other sites More sharing options...
paul2463 Posted February 24, 2007 Share Posted February 24, 2007 // 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"); Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193053 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 // 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. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193055 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 Figured it to work right now. Anways... Can you give me an example when i click a link then it starts downloading? And an example where i get to see all the files in a dir? Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193059 Share on other sites More sharing options...
paul2463 Posted February 24, 2007 Share Posted February 24, 2007 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"); Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193069 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 So how am i gonna echo the files in my download directory for the user to see? Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193071 Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 Woundln't it just be easier to make a .htaccess file and allow directory browsing for the download dir? Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193072 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 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 And what im trying to do is to have a download dir, with many user dir to group users' files in a certain directory. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193073 Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193078 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193093 Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 Yeah kind of.. Just list what you have in a dir and people can just download form it.. If you want to do it so it does what Paul was takling about then its up to you... Pick your flavor.. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193106 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 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. Because a user can only download his created files. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193117 Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193120 Share on other sites More sharing options...
renj0806 Posted February 24, 2007 Author Share Posted February 24, 2007 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. So anyone has any idea how to show files from a specific folder? By show, it means it is seen in a browser. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193125 Share on other sites More sharing options...
magic2goodil Posted February 24, 2007 Share Posted February 24, 2007 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193134 Share on other sites More sharing options...
magic2goodil Posted February 24, 2007 Share Posted February 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193139 Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193147 Share on other sites More sharing options...
TRI0N Posted February 24, 2007 Share Posted February 24, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/39942-php-script-for-file-downloading/#findComment-193155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.