PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Bao Nguyen Quoc   HTTP Download   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Example
Class: HTTP Download
Server files for downloading with resume support
Author: By
Last change: fix some small problem with test script
Date: 19 years ago
Size: 2,962 bytes

Here you can get a sample PHP file download script

Click here to find the PHP file download on this page.

What is a sample PHP file download script

It is a script that can serve a file stored on the server to users. Those who access a page using this script can download the file to their computers, mobile phones, tablets, or other devices.

Where can I find the download file PHP script

You can scroll down this page or click here to view or download the PHP file download script named test.php.

What does the test.php file do

This is a script that can serve files for download.

It uses a httpdownload.php class script to get the contents of a given file on the server and outputs the contents using HTTP headers to pass details about the file to the user's browser that will download the file to the user's computer or mobile, or tablet.

It also supports resuming downloads. If a user started downloading a file and stopped for some reason, this PHP class can resume the download where it stopped.

Can PHP download files from other sites?

Yes, PHP can also download files from any accessible website.

To download files from another site, you can use, for instance, the PHP file_get_contents function, or PHP Curl extension functions, or even and PHP package of classes, like for instance this PHP HTTP client package.

Can I download a PHP file from a website

It depends on what you mean by PHP file. If you mean download the PHP script that generates a given Web page, usually you are not able to download the PHP script itself because when the Web server gets a request to serve a Web page using PHP, it executes the PHP script and returns the HTML generated by that script.

If you want to download a PHP script that generates the Web pages, the author of that PHP script must make it available for download on another page, usually with a link to serve the page for download.

Can I have a PHP test page to download files

Sure. It is always recommended to test your pages that use PHP or just plain HTML before you put the scripts that serve your pages on a production server.

If you can install a Web server and PHP in a development machine, it is a good idea to use that server to test your pages in your development environment.

Can I use an index.php example script to download files

Yes. You can download files using any PHP script. If you want to download files using the test.php script below, just rename it to index.php.

How can I implement my own file download PHP script

The simplest way to make a file available for download is to use the PHP functions file_get_contents and header.

The header function can be used to tell the user's browser the type of file. For instance, if you want to serve a .zip file for download, you use the header function to set the Content-Type: header to application/zip like this:

Header('Content-type: application/zip');

Then, if you want to serve a file named download.zip for download, you can use the file_get_contents() function like this:

echo file_get_contents('download.zip');

Keep in mind that this is a very simple example for you to test. It would be better if you store the download.zip in a directory that only your script is aware of its path to make your download script more secure.

 

Contents

Class file image Download
<?

$FILENAME
= ""; //SET YOUR FILE HERE

if (!$FILENAME || !file_exists($FILENAME)) {
    echo
"Please set your target file \$FILENAME on line 2\n";
    exit();
}

include_once
"class.httpdownload.php";

$object = new httpdownload;

$bandwidth = @intval(implode('',file('bandwidth.txt'))) / 1024;
if (
$bandwidth > 1024)
{
   
$bandwidth = round($bandwidth / 1024 , 2);
   
$bandwidth .= " MB";
}
else
{
   
$bandwidth .= " KB";
}

switch (@
$_GET['download']) {
case
'resume_speed':
case
'noresume_speed':
case
'resume':
case
'noresume':
   
$object->set_byfile($FILENAME);
    if (
$_GET['download'] == 'noresume' || $_GET['download'] == 'noresume_speed') $object->use_resume = false;
    if (
$_GET['download'] == 'resume_speed' || $_GET['download'] == 'noresume_speed' ) $object->speed = 100;
   
$object->download();
break;
case
'data':
case
'dataresume':
   
$data = implode('' , file($FILENAME));
   
$object->set_bydata($data);
    if (
$_SERVER['download'] != 'dataresume') $object->use_resume = false;
   
$object->filename = basename($FILENAME);
   
$object->download();
break;
case
'auth':
   
$object->set_byfile($FILENAME);
   
$object->use_auth = true;
   
$object->handler['auth'] = "test_auth";
   
$object->download();
break;
case
'url':
   
$object->set_byurl('http://www.php.net/get/php_manual_chm.zip/from/cr.php.net/mirror');
   
$object->download();
break;
}

if (
$object->bandwidth > 0)
{
   
error_reporting(E_NONE);
   
$b = intval(implode('',file('bandwidth.txt'))) + $object->bandwidth;
   
$f = fopen('bandwidth.txt','wb');
   
fwrite($f,$b);
   
fclose($f);
    exit;
}

function
test_auth($user,$pass) { //test authentication function
   
if ($user == 'user' && $pass == 'pass') return true;
    return
false;
}

?>

<head>
<style>
<!--
body { font-family: Tahoma; font-size: 12px }
a { color: #FF0000 }
-->
</style>
</head>

<title>HTTPDownload example</title>

<h2><font color="navy">HttpDownload</font></h2>Select a link and try it with a download manager (like <a href="http://reget.com">Reget</a>) .<br><br>

Total bandwidth used : <B><?=$bandwidth?></B>

<br><br>
<a href="test.php?download=noresume">Download file</a><br>
<a href="test.php?download=noresume_speed">Download file (speed limit 100 kbs)</a><br>
<a href="test.php?download=resume">Download file with resume</a><br>
<a href="test.php?download=resume_speed">Download file with resume (speed limit 100 kbs) </a><br>
<a href="test.php?download=data">Download file data (May slow)</a><br>
<a href="test.php?download=dataresume">Download file data with resume (May slow)</a><br>
<a href="test.php?download=auth">Authentication download (user/pass)</a><br>
<a href="test.php?download=url">URL Download (simple redirect)</a><br>

<p><font size="1"><font color="#808080">( Click
<a href="http://en.vietapi.com/wiki/index.php/PHP:_HttpDownload">
<font color="black">here</font></a><font color=""> to view class
information )</font></p>