• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Download script times out....

XeonGX

New Member
Hello,
Today I just discovered that my download script times out. :cry2:
When download reaches 39.9MB it stops,
Can somebody help fix the code?

download.php
PHP:
<? 
include ( "member/db.php" );
include ( "member/settings.php" );
include ( "member/header.php" );
if ( !isset ( $user_info[user_id] ) ) {
header( "Location: $site_path/login" );
exit();
ob_end_flush();
}
###############################################################
# Above part only allows members to execute the script.
###############################################################
?>
<?
session_start( );
if( !$_SESSION['DownloadCount'] and !$_COOKIE['DownloadCount'] ) $_SESSION['DownloadCount'] = 1;

$dl_limit = "5"; # 5
$dl_period = "3600"; # per 1 hour

setcookie("DownloadCount", $_SESSION['DownloadCount'] );

$_SESSION['DownloadCount']++;

if( $_COOKIE['DownloadCount'] < $dl_limit )
    include "dl.php";
else
    echo "<script language=\"javascript\">alert('You have reached or exceeded your download limit')</script>";

?>

dl.php
PHP:
<?php

###############################################################
# File Download 1.1
###############################################################
# Visit http://www.zubrag.com/scripts/ for updates
###############################################################
# Sample call:
#    download.php?f=phptutorial.zip
#
# Sample call (browser will try to save with new file name):
#    download.php?f=phptutorial.zip&fc=php123tutorial.zip
###############################################################

// Download folder, i.e. folder where you keep all files for download.
// MUST end with slash (i.e. "/" )
define('BASE_DIR','/home/joan/backup/');

// log downloads?  true/false
define('LOG_DOWNLOADS',true);

// log file name
define('LOG_FILE','downloads.log');

// Allowed extensions list in format 'extension' => 'mime type'
// If myme type is set to empty string then script will try to detect mime type 
// itself, which would only work if you have Mimetype or Fileinfo extensions
// installed on server.


####################################################################
###  DO NOT CHANGE BELOW
####################################################################

// Make sure program execution doesn't time out
// Set maximum script execution time in seconds (0 means no limit)
set_time_limit(0);

if (!isset($_GET['f']) || empty($_GET['f'])) {
  die("Please specify file name for download.");
}

// Get real file name.
// Remove any path info to avoid hacking by adding relative path, etc.
$fname = basename($_GET['f']);

// Check if the file exists
// Check in subfolders too
function find_file ($dirname, $fname, &$file_path) {

  $dir = opendir($dirname);

  while ($file = readdir($dir)) {
    if (empty($file_path) && $file != '.' && $file != '..') {
      if (is_dir($dirname.'/'.$file)) {
        find_file($dirname.'/'.$file, $fname, $file_path);
      }
      else {
        if (file_exists($dirname.'/'.$fname)) {
          $file_path = $dirname.'/'.$fname;
          return;
        }
      }
    }
  }

} // find_file

// get full file path (including subfolders)
$file_path = '';
find_file(BASE_DIR, $fname, $file_path);

if (!is_file($file_path)) {
  die("File does not exist. Make sure you specified correct file name."); 
}

// file size in bytes
$fsize = filesize($file_path); 

// file extension
$fext = strtolower(substr(strrchr($fname,"."),1));

// check if allowed extension


// get mime type



// Browser will try to save file with this filename, regardless original filename.
// You can override it if needed.

if (!isset($_GET['fc']) || empty($_GET['fc'])) {
  $asfname = $fname;
}
else {
  $asfname = $_GET['fc'];
}

// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

// download
@readfile($file_path);

// log downloads
if (!LOG_DOWNLOADS) die();

$f = @fopen(LOG_FILE, 'a+');
if ($f) {
  @fputs($f, date("m.d.Y g:ia")."  ".$_SERVER['REMOTE_ADDR']."  ".$fname."\n");
  @fclose($f);
}

?>
 
firstly, you should only really log the download if it completes, readfile returns the bytes read and so wrap that in a condition before you set cookies, also, if your connection is pretty slow then that script will timeout on most servers because theres no set_time_limit( 0 ); try those things aswell as making sure your post_max_size and upload limits are higher than you need
 
on the server side yeah sure, but if post_max_size is too small or the connection your using to upload is too slow there's nothing you can do about your code timing out

post the other script, is bedtime for me, I'll have a look in the am ... however I don't see that there can be much of anything else wrong because there is only one way to upload files - and it works with small ones, so that pretty much eliminates the code, leaving the user and the server configuration only....
 
do u konw another script that does the same thing?
for example
im having the actual files in another folder,
and i link to their download like dl.php?file=123.ZIP
and the person whos downloading won't know what folder the files are in...
 
PHP:
<?php
session_start( );

if( !defined('DLIMIT') ) define('DLIMIT',			5 );

if( !defined('DTIME') ) define('DTIME',				3600 );

if( !defined('DLOC')) define('DLOC',				'/home/joan/backup' );

if( !defined('ONSUCCESS')) define('ONSUCCESS',		'/my/success.php' );

if( !defined('ONFAIL')) define('ONFAIL',			'/my/failure.php' );

include ( 'member/db.php' );
include ( 'member/settings.php' );
include ( 'member/header.php' );

function find_file( $location, &$find )
{	
	if( is_dir( $location ) and ( $directory = opendir( $location ) ) )
	{	
		while( $file = readdir( $directory ) )
		{
			if( $file != "." and $file != ".." )
			{
				if( is_dir(sprintf( '%s/%s', $location, $file ) ) )
				{
					find_file( sprintf( '%s/%s', $location, $file ), $find );
				}
				elseif( $find == basename( sprintf( '%s/%s', $location, $file ) ) )
				{
					$find = sprintf( '%s/%s', $location, $file ) ;
					break;		
				}
			}	
		}
		closedir( $directory );	
	}		
}
function is_found( $was, $is )
{
	return $was == $is ? false : true ;
}
function logging( $address, $file, $stamp = 'm.d.Y g:ia'  )
{
	if ( LOG_DOWNLOADS )
	{
		if ( ( $write = @fopen( LOG_FILE, 'a+' ) ) )  
		{
		  $written = @fputs( $write, sprintf(
		  	'%s %s %s\n',
		  	date( $stamp ),
		  	$address,
		  	$file
		  ) );
		  @fclose( $write );
		}	
		return $written ;
	}	
}
function download( $file )
{
	header( "Pragma: public" );
	header( "Expires: 0" );
	header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
	header( "Cache-Control: public" );
	header( "Content-Description: File Transfer" );
	header( sprintf( "Content-Type: %s", content_type( $file ) ) );
	header( sprintf( 'Content-Disposition: attachment; filename="%s"', basename( $file ) ) );
	header( "Content-Transfer-Encoding: binary" );
	header( sprintf( "Content-Length: %d", filesize( $file ) ) );
	return readfile( $file ) == filesize( $file ) ? true : false ;
}
// not sure how you're doing that
function content_type( $file )
{
	return 'application-x/zip';
}

if ( !isset ( $user_info['user_id'] ) ) 
{
	header( sprintf( "Location: %s/login", $site_path ) );
	ob_end_flush();
	exit();
}

$_SESSION['dlcount'] = $_SESSION['dlcount'] ? $_SESSION['dlcount'] : $_COOKIE['dlcount'] ? $_COOKIE['dlcount'] : 0 ;

if( !( $file = @basename( $_GET['f'] )) ) die("Please specify file name for download.");	

$temp = $file ;

find_file( DLOC, $file );

if( !is_found( $temp, $file ) )
{
	die( sprintf( 'Cannot find %s, plesae try again', $file ) );
}
unset( $temp );

if( download_file( $file ) )
{
	logging( $_SERVER['HTTP_HOST'], $file );
	$_SESSION['dlcount']++ ;
	header( sprintf( 'Location: %s', ONSUCCESS ) );
	exit;
}
else
{
	header( sprintf( 'Location: %s', ONFAIL ) );
	exit;
}

Check through the code, make sure everything is defined the way you wannit, and try that ..... you might get a couple of errors, is totally untested .....
 
Fatal error: Call to undefined function: download_file() in /home/user2154/public_html/down.php on line 101


EDIT:
i just found out that the old script works,
dl.php?f=file.zip works fine, but when i want to add
PHP:
<? 
include ( "member/db.php" );
include ( "member/settings.php" );
include ( "member/header.php" );
if ( !isset ( $user_info[user_id] ) ) {
header( "Location: $site_path/login" );
exit();
ob_end_flush();
}
###############################################################
# Above part only allows members to execute the script.
###############################################################
?>
<?
session_start( );
if( !$_SESSION['DownloadCount'] and !$_COOKIE['DownloadCount'] ) $_SESSION['DownloadCount'] = 1;

$dl_limit = "5"; # 5
$dl_period = "3600"; # per 1 hour

setcookie("DownloadCount", $_SESSION['DownloadCount'] );

$_SESSION['DownloadCount']++;

if( $_COOKIE['DownloadCount'] < $dl_limit )
    include "dl.php";
else
    echo "<script language=\"javascript\">alert('You have reached or exceeded your download limit')</script>";

?>
it messes up the script,
question:
how can i put dl.php into download.php without having to include it as a seprate file.

PHP:
if( $_COOKIE['DownloadCount'] < $dl_limit )
    include "dl.php";
 
Last edited:
Just

PHP:
if( $_COOKIE['DownloadCount'] < $dl_limit )
{
 // code cut and paste from dl.php
}
else
{
 // whatever
}

There's no difference between that and your code as it is.....

Errors because I named the function download() and not download_file() try changing it to download if you're still stuck.....
 
Back
Top