Why is this zip file empty?

Bruce Gust
Bruce Gust used Ask the Experts™
on
I've got a link that looks like this: http://loosecannonfitness.com/downloads.php?email=bruce@brucegust.com

It hits that page and, provided it gets appropriately validated, it hits this code:

$michelle_row=mysqli_fetch_assoc($michelle_query);
	$vivian="update hammons set download_success='Y' where email='$michelle_row[email]'";
	$vivian_query=mysqli_query($cxn, $vivian);
	if($michelle_row['workout']=="1775")
	{
		$filename="1775.zip";
	}
	else
	{
		$filename="Devildog.zip";
	}
	header("Content-Type: application/zip");
	header('Content-Disposition: attachment; filename='.$filename);

Open in new window


The "update" part works great and I get the box that asks if I want to save or open the zip file. The problem is, although I can look at my server and see the "1775.zip" file that's over 22MB, when I go to download it, it's 0 bytes.

In other words, everything seems to be in place except for the content of the file.

What am I doing wrong?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you need to send the file to the client

add this at the end of your script

$fp = fopen("FILENAME", 'rb');
fpassthru($fp);
Bruce GustPHP Developer

Author

Commented:
First of all, I think your "skullnobrains" is both hilarious in that it's an obvious misnomer.

Secondly, there are two questions I have:

If the whole of my script looks like this:

<?php
include("carter.inc"); 
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
$michelle="select * from hammons where email='$_GET[email]' and download_success<>'Y'";
$michelle_query=mysqli_query($cxn, $michelle);
if(!$michelle_query)
{
	$rats=mysqli_errnor($cxn).': '.mysqli_error($cxn);
	die($rats);
}
$michelle_count=mysqli_num_rows($michelle_query);
if(!$michelle_count>0)
{
	header("location:downloads_fail.php");
	exit();
}
else
{
	$michelle_row=mysqli_fetch_assoc($michelle_query);
	$vivian="update hammons set download_success='Y' where email='$michelle_row[email]'";
	$vivian_query=mysqli_query($cxn, $vivian);
	if($michelle_row['workout']=="1775")
	{
		$filename="1775.zip";
	}
	else
	{
		$filename="Devildog.zip";
	}
	header("Content-Type: application/zip");
	header('Content-Disposition: attachment; filename='.$filename);
}
?>

Open in new window


Where does your suggestion belong?

Second question: I'm not familiar fpassthru. I went out to the manual (http://php.net/manual/en/function.fpassthru.php) and found this:  

Reads to EOF on the given file pointer from the current position and writes the results to the output buffer.

Could you put that in layman's terms and explain to me what that means?

I appreciate you, friend! Glad you're up and running on a Saturday!
Most Valuable Expert 2011
Top Expert 2016
Commented:
layman's terms...
It sends the file line-by-line as if it were browser output, written with echo.

An alternative to consider if you won't want to send the whole file in the response - you could write a copy of the file into the server file system and just send a link to the client.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Please check whether zip file is available ,
// Your previous code
	if(!file_exists($filename))
	{
	   echo "File does not exist";  
	}
	else if (!is_readable($filename))
	{
	   echo 'File not readable';
	}    
	else
	{
  	        header("Content-Type: application/zip");
		header("Content-Transfer-Encoding: Binary");
		header("Content-Length: ".filesize($filename));
		header('Pragma: no-cache');
		header("Content-Disposition: attachment; filename=\"".basename($filename)."\"");
		readfile($filename);
	}
}
?>

Open in new window

Bruce GustPHP Developer

Author

Commented:
Thank you!
It sends the file line-by-line as if it were browser output, written with echo.

ray:

not quite. it acts the same as readfile (which is simpler btw, thanks @insoft for making it one line shorter) or stream copy to stream would act.

they copy the file block by block. the file is moved as fast as the disk and network can handle. the read side is mmapped and the write side is sent to the php output buffer which itself if flushed asynchronously to the webserver's / the system's buffers. in real life, on what i assume to be a lamp server, the file is mmapped by increments of 2 4k blocks at a time and flushed to apache's buffer ( which is large enough to handle the whole file in this case ). this is world's appart more efficient that reading line by line when it comes to zip files.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial