Jump to content

Recommended Posts

I'm trying to make users download a file, but they must wait 60 seconds before it begins.

 

But, I'm stuck - the file isn't downloading, and no error is being shown (I've enabled E_ALL error reporting).

 

The file does exist.

 

Here is the code I use to download the file (I've cut the code that isn't needed)

<?php
//code removed
case "dlStart":
	$file = $_POST['fileid'];
	//query
	$query = "SELECT * FROM files WHERE fid=$file";
	$result = $sql->query($query);
	$row = $result->fetch_assoc();
	$file = $row['filelocation'];
	if ($result->num_rows != 1){
		$content = '<h1>Download File</h1>
		<h2>Looks like you&#39;ve found an error!</h2>
		<p>The file that you are trying to download either has been deleted, or has never existed.</p>
		<p>Please go back and try again.</p>';
	$content .= '<p style="text-align: center; ">Powered by <a href="http://www.scriptboxer.com/download-site-script.php">DownloadBoxer v1</a>.<br />
	© <a href="http://www.scriptboxer.com">ScriptBoxer</a> - <a href="http://www.scriptboxer.com">PHP Scripts</a></p>';
	$html = str_replace('{content}', $content, $html);
	$html = str_replace('{title}', 'Downloading File....', $html);
	echo $html;
	}else{
		$content = '<form name="counter"><h1>Download File</h1>
			<h2>Your download will begin in:
			<input name="d2" size="1" type="text" style="font-family:Segoe UI, Trebuchet MS, Helvetica, sans-serif; font-size:23px; font-weight:bold;" /> seconds</h2>
			</form>
			<script> 
			<!-- 
			// 
			var milisec=0 
			var seconds=60 
			document.counter.d2.value=\'30\' 

			function display(){ 
			if (milisec<=0){ 
			milisec=9 
			seconds-=1 
			} 
			if (seconds<=-1){ 
			milisec=0 
			seconds+=1 
			} 
			else 
			milisec-=1 
			document.counter.d2.value=seconds+"."+milisec 
			setTimeout("display()",100) 
			} 
			display() 
			--> 
			</script>
			<p><span style="font-size:11px;">Clicking adverts have no affect on the loading of the file.</span></p>
			<p><a href="?action=donateToUs">Our service is free. Click here to keep it that way.</a></p>
			<p><strong>File uploaded by</strong>: '.$row['owner'].'</p>
			<p><strong>Why do I have to wait to download my file?</strong><br />
			We require users to wait before downloading files, so that we can reduce the load on the server. Sometimes, you will be able to download immediately, 
			other times you will need to wait upto 20 minutes.</p>
			<p><a href="?action=reportFile">CLICK HERE TO REPORT ILLEGAL/PIRATE FILES</a></p>';
	function output_file($file, $name, $mime_type='')
	{
	if(!is_readable($file)) die('File not found or inaccessible!');
	$size = filesize($file);
	$name = rawurldecode($name);
	/* Figure out the MIME type (if not specified) */
	$known_mime_types=array(
		"txt" => "text/plain",
		"zip" => "application/zip",
		"doc" => "application/msword",
		"xls" => "application/vnd.ms-excel",
		"gif" => "image/gif",
		"png" => "image/png",
		"jpeg"=> "image/jpg",
		"jpg" =>  "image/jpg",
	 );

	if($mime_type==''){
		$file_extension = strtolower(substr(strrchr($file,"."),1));
		if(array_key_exists($file_extension, $known_mime_types)){
			$mime_type=$known_mime_types[$file_extension];
		} else {
			$mime_type="application/force-download";
		};
	};
	@ob_end_clean(); //turn off output buffering to decrease cpu usage
	// required for IE, otherwise Content-Disposition may be ignored
	if(ini_get('zlib.output_compression'))
	ini_set('zlib.output_compression', 'Off');

	header('Content-Type: ' . $mime_type);
	header('Content-Disposition: attachment; filename="'.$name.'"');
	header("Content-Transfer-Encoding: binary");
	header('Accept-Ranges: bytes');
	header("Cache-control: private");
	header('Pragma: private');
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	// multipart-download and download resuming support
	if(isset($_SERVER['HTTP_RANGE']))
	{
		list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
		list($range) = explode(",",$range,2);
		list($range, $range_end) = explode("-", $range);
		$range=intval($range);
		if(!$range_end) {
			$range_end=$size-1;
		} else {
			$range_end=intval($range_end);
		}
		$new_length = $range_end-$range+1;
		header("HTTP/1.1 206 Partial Content");
		header("Content-Length: $new_length");
		header("Content-Range: bytes $range-$range_end/$size");
	} else {
		$new_length=$size;
		header("Content-Length: ".$size);
	}
	/* output the file itself */
	$chunksize = 1*(10241024*10241024); //you may want to change this
	$bytes_send = 0;
	if ($file = fopen($file, 'r'))
		{
		if(isset($_SERVER['HTTP_RANGE']))
			fseek($file, $range);
			while(!feof($file) && 
			(!connection_aborted()) && 
			($bytes_send<$new_length)
			)
		{
			$buffer = fread($file, $chunksize);
			print($buffer); //echo($buffer); // is also possible
			flush();
			$bytes_send += strlen($buffer);
		}
		fclose($file);
	} else die('Error - can not open file.');
	die();
	}
	}
	$content .= '<p style="text-align: center; ">Powered by <a href="http://www.scriptboxer.com/download-site-script.php">DownloadBoxer v1</a>.<br />
		© <a href="http://www.scriptboxer.com">ScriptBoxer</a> - <a href="http://www.scriptboxer.com">PHP Scripts</a></p>';
		$html = str_replace('{content}', $content, $html);
		$html = str_replace('{title}', 'Downloading File....', $html);
		echo $html;
	break;
?>

 

The files are located outwith the public directory, and the MySQL db has the full location stored in it (as the format /home/bleh/blah/etc)

 

Can anyone help?

 

I'd also like the download to begin after $config['waittime'], so how do I do that aswell?

Link to comment
https://forums.phpfreaks.com/topic/178025-downloading-file-headers/
Share on other sites

Your code is a mess, so instead of filtering thought and trying to guess the problem, can you give a little more info..

 

you say the file isn't downloading.. okay what is happening ?

blank page error ?

have you tried debugging ?

where does your code fail ?

Your code is a mess, so instead of filtering thought and trying to guess the problem, can you give a little more info..

 

you say the file isn't downloading.. okay what is happening ?

blank page error ?

have you tried debugging ?

where does your code fail ?

 

Just shows the $content content for a file found.

 

Nope, not a blank page.

 

Debugging shows that fetch_assoc is not working, so I'll check that.

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.