任何文件大小的PHP下载脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了任何文件大小的PHP下载脚本相关的知识,希望对你有一定的参考价值。
By default Apache won't allow you to download a file over 2GB. This php script overcomes that along with some extra goodies.The script will read from standard url path the file on the server to download and display the filesize along with a link to download it.
Setup:
1) Create a directory called "d" on the root of your website and name this script "index.php"
2) Create a .htaccess file with the following lines:
RewriteEngine On
RewriteBase /d/
RewriteCond %{REQUEST_DIRECTORY} !-d [NC]
RewriteRule ^(.*)$ index.php?f=$1 [L]
3) Craft a url in the format of: http://[website_url]/d/path/to/download/filename.dat
This will link to a file: http://[website_url]/path/to/download/filename.dat
If you want, you can also craft a url with a trailing "&" and it will automatically start downloading.
Example: http://[website_url]/path/to/download/filename.dat&
<?php // Use PHP to serve files too big for Apache to deliver (>2GB) function serveFile($file){ $as = $path_parts['basename']; // Exec to determine correct file size (see's bigger files than PHP can see) { // send the current file part to the browser // flush the content to the browser } } else { print "File not found"; } } $file = "../" . str_replace(array("/d/", "?f=","../"), "", trim(urldecode($_SERVER['REQUEST_URI']))); // If the last character of the URL is & then trigger for Download $download = true; // Strip off & from the end of the $file variable }else{ $download = false; } print "Click to download <a href="/d/?f=".$url_path."&">".$path_parts['basename']."</a>"; print " (".$size." bytes)"; } else { print "File not found"; } } else { serveFile($file); } ?>
以上是关于任何文件大小的PHP下载脚本的主要内容,如果未能解决你的问题,请参考以下文章