Why is this zip file empty?

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?
Bruce GustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skullnobrainsCommented:
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 DeveloperAuthor 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!
Ray PaseurCommented:
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.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

InsoftserviceCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
Thank you!
skullnobrainsCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.