Uploaded download script doesn't work.

cvsubelm

New Member
Messages
2
Reaction score
0
Points
0
Hi,


I need help for my script to work fine.


I have my download script on my xampp (PHP version 5.3.5) and it is working fine.
Here is the code and some screenies.


in dl_functions.php
PHP:
<?

function set_size($size)
{
	$kb = 1024;         
	$mb = 1024 * $kb;   
	$gb = 1024 * $mb;   
	$tb = 1024 * $gb;   
	if ($size < $kb) return $size.' B';
	elseif ($size < $mb) return round($size/$kb,2).' KB';
	elseif ($size < $gb) return round($size/$mb,2).' MB';
	elseif ($size < $tb) return round($size/$gb,2).' GB';
	else return round($size/$tb,2).' TB';
}

function get_download_files_list()
{
 global $download_dirs, $SLINE;
	$files_list = ""; 
	$i = 0;
	$from_path = dirname(__FILE__)."/".$download_dirs;
	
	if (is_dir($from_path)) {
		chdir($from_path);
		$handle = opendir('.');
		while (($file = readdir($handle)) !== false)
		{
			if (($file != ".") && ($file != "..") && is_file($file)) {
				
				$files_list .= '<tr><td width="75%"><a href="viewfiles.php?'.$SLINE.'&file='.htmlentities(urlencode(basename($file))).'">'.basename($file).'</a></td><td>'.set_size(filesize($file)).'</td><td><input type="checkbox" name="deleteMe[]" value="'.basename($file).'"></tr>';
			}
		}
		closedir($handle); 
	}
 return $files_list;
}

function do_download($file)
{
 global $download_dirs;
	$file_name = dirname(__FILE__)."/".$download_dirs.$file;
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
	header("Pragma: public");
	header("Content-Type: application/save");
	header("Content-Length: ".filesize($file_name));
	header("Content-Disposition: attachment; filename=\"$file\"");
	header("Content-Transfer-Encoding: binary");
	readfile($file_name);
	exit;
}

?>


and in viewfiles.php
PHP:
<?php 
include 'include/session.php';
include 'templates/include/header.php';
include 'include/menubar.php';

include("config.php");
include("dl_functions.php");


$from_path = dirname(__FILE__)."/".$download_dirs;
	
if (is_dir($from_path)) {
	chdir($from_path);
	$handle = opendir('.');
	while (($file = readdir($handle)) !== false)
	{
		if (($file != ".") && ($file != "..") && is_file($file)) {
			if(isset($_POST['deleteMe']) && count($_POST['deleteMe']) > 0){
				foreach($_POST['deleteMe'] as $file){
					if(file_exists($from_path.$file)){
						unlink($from_path.$file);
					}
				}
			}

        }
		
	}
	//unset($_POST['deleteMe']);
}
    

if (isset($_GET["file"])) do_download($_GET["file"]);

?>
<div id="container">

<div id="content">
<h1>VIEW FILES</h1>
<a href="admin.php">Back</a>
<?php if($session->isAdmin()){
	echo '|&nbsp;<a href="upload.php">Upload Files</a>';
}
if($session->isInstructor()){
	echo '|&nbsp;<a href="upload.php">Upload Files</a>';
}
?>
<br />
<form action="viewfiles.php" method="post">
<table class="main_table" align="center" style="width: auto;">
  <tr>
    <td colspan="1"><b>Download files list</b></td>
    <td colspan="1"><b>Size</b></td>
    <td colspan="1"><b>Select</b></td>
  </tr>
  <?php echo get_download_files_list(); ?>
</table>
<center><button type="submit" name="submit" value="submit" onclick="return confirm('Note: This will delete selected files. None otherwise.')">Delete selected</button>
<button type="reset" name="reset">Reset</button></center>
</form>

<br />
</div>
<?php include 'templates/include/footer.php'; ?>
</div>


I uploaded my site into x10hosting.com (w/ PHP version 5.3.10), after i clicked the file to be downloaded(it supposed to be force download), it gave this random characters(See screenshot)
441s.png



Is the webhosting affects download scripts with their file permissions, mimetype, headers, or content-encoding?


This just works fine in my xampp. please help. Thank you.


REGARDS,
MARK B.
 
Last edited:

mraz

New Member
Messages
95
Reaction score
3
Points
0
have you tried adding the header below?

header("Content-type: application/force-download");
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You're already setting the header when you include header.php, so trying to set the content-type header later isn't going to work. On your local server, output buffering is probably enabled, so having character output queued up and ready to go isn't a problem; it is silently discarded when the script decides that you actually want the file content. Here, the HTML output is sent immediately, so the content of the file is appended to the already-included page header.

You have two choices: either turn on output buffering using ob_start() (see the PHP manual for usage, including flush and clean methods), or move the header include to a place on the page where it cannot interfere with other output (immediately before any additional HTML content is output). Using output buffering means having to remember to send or discard the content of the buffer; moving the include may mean separating it into two or more files so that some variables are immediately available while any HTML content is deferred until it is needed.
 

cvsubelm

New Member
Messages
2
Reaction score
0
Points
0
You're already setting the header when you include header.php, so trying to set the content-type header later isn't going to work. On your local server, output buffering is probably enabled, so having character output queued up and ready to go isn't a problem; it is silently discarded when the script decides that you actually want the file content. Here, the HTML output is sent immediately, so the content of the file is appended to the already-included page header.

You have two choices: either turn on output buffering using ob_start() (see the PHP manual for usage, including flush and clean methods), or move the header include to a place on the page where it cannot interfere with other output (immediately before any additional HTML content is output). Using output buffering means having to remember to send or discard the content of the buffer; moving the include may mean separating it into two or more files so that some variables are immediately available while any HTML content is deferred until it is needed.

Oh gosh, i forgot the first rule about headers XD, and i didn't knew that output buffering is disabled on x10hosting's server. I see.

Lately I tried to put ob_start(); in dl_functions.php, but i realized now there is no html in that file haha XD.

Thanks! I put that ob functions in viewfiles.php where html is written. And it worked just like in my xampp server! :DDDDDD

Regards,
Mark B.
 
Top