Jump to content

Download program only sends first 1kb


Lassie

Recommended Posts

I have modified a download program to suit my purposes, and it essentailly works except I only get the first 1Kb regardless of file type.

Below is the download section of the code. Where should I look for the problem?Is there anything in this code that would produce that type of result?

 


# If a download has already been selected,
#----------------------------------------- 
If ($_GET['file']){

# Get the filename to be downloaded
#----------------------------------
$file = $_GET['file'];
$file = stripslashes($file);
$file = htmlspecialchars($file);
$mainpath = "$path$file";

# Increment download count
#-------------------------
$filename = "./file_info/descriptions/$file.0";
$newfile = fopen($filename,"r");
$content = fread($newfile, filesize($filename));
fclose($newfile);
$fileinfo = explode ("|",$content);
$fileinfo[0] ++;

# Save the new file count
#------------------------
$content = implode("|", $fileinfo);
$newfile = fopen($filename,"w");
fwrite($newfile, $content);
fclose($newfile);

# Translate file name properly for Internet Explorer
#---------------------------------------------------
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
	$file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1);
}

# Make sure the file exists before sending headers
#-------------------------------------------------
if(!$fdl=@fopen($mainpath,'r')){
   die("Cannot Open File!");
} else {

# Send the headers then send the file
#------------------------------------
header("Cache-Control: ");# leave blank to avoid IE errors
header("Pragma: ");# leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-length:".(string)(filesize($mainpath)));
sleep(1);
fpassthru($fdl);
}

}else{

# GD Library thumbnail function.
#-------------------------------
function thumbnail($i,$nw,$p,$nn) {

    		$img=imagecreatefromjpeg("$i");
    		$ow=imagesx($img);
    		$oh=imagesy($img);
    		$scale=$nw/$ow;
    		$nh=ceil($oh*$scale);
    		$newimg=imagecreateTrueColor($nw,$nh);
    		imagecopyresized($newimg,$img,0,0,0,0,$nw,$nh,$ow,$oh);
    		imagejpeg($newimg, $p.$nn);
    		$heigth = $nh;
    		return ($heigth);
}

Link to comment
https://forums.phpfreaks.com/topic/53433-download-program-only-sends-first-1kb/
Share on other sites

Thanks for the advice. I have tried your suggestion but I get no print output.

I am not certain that I have coded this correctly but I will post the code so that you might venture an opinion.

Thanks

# Send the headers then send the file
#------------------------------------
header("Cache-Control: ");# leave blank to avoid IE errors
header("Pragma: ");# leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-length:".(string)(filesize($mainpath)));
sleep(1);
fpassthru($fdl);
echo '(filesize[$mainpath]})';
}

Looking at this code you use "$mainpath" when that is defined but poorly. $filename should be the one you use as you use it for fread. Try the code below and see what is printed out, if you get an output than $filename is the one to use and replace that for $mainpath.


# If a download has already been selected,
#----------------------------------------- 
If ($_GET['file']){

# Get the filename to be downloaded
#----------------------------------
$file = $_GET['file'];
$file = stripslashes($file);
$file = htmlspecialchars($file);
$mainpath = "$path$file";

# Increment download count
#-------------------------
$filename = "./file_info/descriptions/$file.0";
        die(filesize($filename));   // if you get output remove this line and replace $mainpath with $filename
$newfile = fopen($filename,"r");
$content = fread($newfile, filesize($filename));
fclose($newfile);
$fileinfo = explode ("|",$content);
$fileinfo[0] ++;

# Save the new file count
#------------------------
$content = implode("|", $fileinfo);
$newfile = fopen($filename,"w");
fwrite($newfile, $content);
fclose($newfile);

# Translate file name properly for Internet Explorer
#---------------------------------------------------
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
	$file = preg_replace('/\./', '%2e', $file, substr_count($file, '.') - 1);
}

# Make sure the file exists before sending headers
#-------------------------------------------------
if(!$fdl=@fopen($mainpath,'r')){
   die("Cannot Open File!");
} else {

# Send the headers then send the file
#------------------------------------
header("Cache-Control: ");# leave blank to avoid IE errors
header("Pragma: ");# leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-length:".(string)(filesize($mainpath))); // if you get output remove this line
header("Content-length:".(string)(filesize($filename)));  // if you do not get output remove this line and do some more testing.
sleep(1);
fpassthru($fdl);
}

}else{

# GD Library thumbnail function.
#-------------------------------
function thumbnail($i,$nw,$p,$nn) {

    		$img=imagecreatefromjpeg("$i");
    		$ow=imagesx($img);
    		$oh=imagesy($img);
    		$scale=$nw/$ow;
    		$nh=ceil($oh*$scale);
    		$newimg=imagecreateTrueColor($nw,$nh);
    		imagecopyresized($newimg,$img,0,0,0,0,$nw,$nh,$ow,$oh);
    		imagejpeg($newimg, $p.$nn);
    		$heigth = $nh;
    		return ($heigth);
}

 

 

Thank you for your suggestion. I now download zero bytes. To add some clarity I am posting some of what I think is relevant code.

I still have no print out of the $mainpath.Is my code OK to echo it as follows?

# Send the headers then send the file
#------------------------------------
header("Cache-Control: ");# leave blank to avoid IE errors
header("Pragma: ");# leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-length:".(string)(filesize($mainpath)));
header("Content-length:".(string)(filesize($$filename)));
sleep(1);
fpassthru($fdl);
echo '(filesize[$mainpath]})';
echo '(filesize[$filename]})';
}

 

variable set at start.
$path = './';
$path = $path.$hash;

# Read current directory.
#------------------------
$d = opendir($path);
while (false !== ($file = readdir($d))) {
	if ($file != "." && $file != "..") {

		# Get all the file attributes.
		#-----------------------------
		$size = filesize("$path$file");
      			$type = filetype("$path$file");
      			$ext = strrchr("$path$file",'.');
		$modified = stat("$path$file");
		$displayname = str_replace (strrchr ($file, "."), "", $file);

# If the file does exist, read its data.
				#---------------------------------------
				$newfile = fopen($filedescription,"r");
				$content = fread($newfile, filesize($filedescription));
				fclose($newfile);
			}
								$data = explode("|", $content);
			$accesses = $data[0];
			$upload_date = $data[3];

# Start main loop.
#-----------------

if ($match > 0){
   		while (list ($key, $val) = each ($filename)) {
		if ($key >= ($records_per_page-$records_per_page)+(($currentpage-1)*$records_per_page) && $key <= ($records_per_page-1)+(($currentpage-1)*$records_per_page)){
			$fileattr = explode("|", $val);

			# Fix and format Byte Length
			#---------------------------
			if ($fileattr[4] < pow(2,10)){
				$size = "$fileattr[4] B";
			}
			if ($fileattr[4] >= pow(2,10) && $fileattr[4] < pow(2,20)) {
				$size = round($fileattr[4] / pow(2,10), 2)." KB";
			}
			if ($fileattr[4] >= pow(2,20) && $fileattr[4] < pow(2,30)) {
				$size = round($fileattr[4] / pow(2,20), 2)." MB";
			}
			if ($fileattr[3] > pow(2,30)) {
				$size = round($fileattr[4] / pow(2,30), 2)." GB";
			}

			# Alternate colors between white and grey (or black and grey) on table entries.
			#------------------------------------------------------------------------------
			if ($color == $color1){
				$color = $color2;
			}ELSE{
				$color = $color1;
			}

			# Get correct icon file depending on file extention.
			#---------------------------------------------------
			$ext = substr ($fileattr[3], 1, 3);
			$ext = (strtolower($ext));
			$iconfile = "./file_info/thumbs/th_$fileattr[2]";
			$nw = 100;
			$nh = 64;
			$xtrainfo = "Jpeg image ";
			if (($ext == "jpg") && (!file_exists($iconfile)) && ($gdlib == 1)){
				$ht = thumbnail("$path$fileattr[2]",$thumbsize,"./file_info/thumbs/","th_$fileattr[2]");
			}

			# Get the correct icon file if image exists with same name as filename.
			#----------------------------------------------------------------------
			if (($ext <> "jpg") or ($gdlib == 0)){
				$xtrainfo = "$ext file";
				$iconfile = "file_info/icons/$icons/x.gif";
				$iconfile1 = "file_info/icons/$icons/$ext.gif";
				$iconfile2 = "file_info/thumbs/th_$fileattr[1].jpg";
				$nw = 64;
				$nh = 64;

				if (file_exists($iconfile1)) {
					$iconfile = $iconfile1;
				}
				if (file_exists($iconfile2)) {
					$iconfile = $iconfile2;
				}
			}
/*				if ($admin){
				$admininfo = "<a href=\"./file_info/admin/edit.php?file=$fileattr[2]\" onClick=\"popup = window.open('./file_info/admin/edit.php?file=$fileattr[2]', 'PopupPage', 'height=350,width=520,scrollbars=yes,resizable=yes'); return false\" target=\"_blank\">Click Here to edit this entry</a><br>";
			}
*/
			# Print html results.
			#--------------------

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.