Hi,

Ok.. i have some pages on my site that has a link like

<A HREF="<?php $websiteaddress ?>/webbuilder/downloads/download.php?filename=age_verification_popup.zip">Download</A>

if you look carefully you will see using get in my below script i get the filename in this example age_verification_popup.zip

This is then sent to this script.

<?php   
        @include ("/home/sites/genieuk.co.cc/public_html/includes/global.php");
        
        // check if member is logged in before issuing file. Function in global.php
        memberonlyzoneerror();   // if user not logged in the function exits and Does Not carryon with script.
        
        // get filename
        $getfile = $_GET['filename'];
        
        // path to file
        $filepath = '/home/sites/mydomain.co.cc/public_html/webbuilder/downloads/files/'.$getfile.'';
        
        // check file exisits, if it does force download/header
        if ( file_exists( $filepath ) ) {
        
        // Headers
        header('Content-type: application/zip');
        header('Content-disposition: attachment; filename='.$getfile.'');
        header("Content-length: " . filesize( $getfile )); 
        
        } else { // else if file does not exist tell them and exit.
            @include ("/home/sites/mydomain.co.cc/public_html/includes/webbuilder_top.inc");
            echo "<h2> File Does Not exist </h2>";
            echo "<p> The file you have requested does not exist. If you feel you received this message in error, please contact us.</p>";
            redirect ("web_builder_download_extensions_templates.php", "3");
            @include ("/home/sites/mydomain.co.cc/public_html/includes/footer.inc");
            exit;
        }
    
 
 
?>

I removed my actual domain as site is not live yet and don't want it revealed. Problem i am having is that when i click link to download it works like it suppose to, passed file name to download.php and then download.php to do it' work, it DOES download the file requested but the file seems empty as when i try and open in Windows Vista using WinRAR i get:

The archive is either in unknown format or is damaged. Now i downloaded file via ftp and it opens so definately not the zip file.

I am at a loss why this is not working for me. Directories etc is correct and fine of course as it downloads etc.

I get no errors or anything, i am sure i am missing something really simple or just not doing something correctly.

I did have file_get_contents in the script but dont think it was needed as after reading about it, it seems only fine if opening/reading the file, althou i did try incase and it did not work that way either.

Can someone please tell me what i am doing wrong please as i am at a complete loss. Been try to figure out what i been doing wrong for hours with no luck.

Thank you,
Mat

Check this line: header("Content-length: " . filesize( $getfile ));

Maybe filesize cannot find your file and returns 0. You may have to use $filepath instead. I think you need to output the file contents too, so put that code back in.

Hey.

Yea, you are using the wrong variable for the Content-Length header, and you don't actually output the file, which is why it is empty.

Line #19 should read:

header("Content-length: " . filesize( $filepath )); 
readfile($filepath); // <-- this sends the file

Check this line: header("Content-length: " . filesize( $getfile ));

Maybe filesize cannot find your file and returns 0. You may have to use $filepath instead. I think you need to output the file contents too, so put that code back in.

Hi i changed my code to this and still no luck, also $getfile and $filepath, altering it a little and it just don't work.

It downloads but is zerobytes in size.

I tried in FireFox latest version, IE 7 and IE8 and still nothing.

<?php	
		@include ("/home/sites/genieuk.co.cc/public_html/includes/global.php");
		
		// check if member is logged in before issuing file. Function in global.php
		memberonlyzoneerror();	
		
		// get filename
		$getfile = $_GET['filename'];
		
		// path to file
		$filepath = '/home/sites/genieuk.co.cc/public_html/webbuilder/downloads/files/'.$getfile.'';
		
		// check file exisits, if it does force download/header
		if ( file_exists( $filepath ) ) {
			
		
		file_get_contents( $filepath );
		
        // Headers
		header('Content-type: application/zip');
		header('Content-disposition: attachment; filename='.$getfile.'');
		header("Content-length: " . filesize( $getfile )); 
		
		} else { // else if file does not exist tell them and exit.
			@include ("/home/sites/genieuk.co.cc/public_html/includes/webbuilder_top.inc");
			echo "<h2> File Does Not exist </h2>";
			echo "<p> The file you have requested does not exist. If you feel you received this message in error, please contact us.</p>";
			redirect ("web_builder_download_extensions_templates.php", "3");
			@include ("/home/sites/genieuk.co.cc/public_html/includes/footer.inc");
			exit;
		}
	


?>

Hey.

Yea, you are using the wrong variable for the Content-Length header, and you don't actually output the file, which is why it is empty.

Line #19 should read:

header("Content-length: " . filesize( $filepath )); 
readfile($filepath); // <-- this sends the file

Hi Alti,

I already tried this,

I will try again.

Thanks
Mat

Hi Alti,

OMG, i tried this earlier and it did not work using the $filepath.

Oh dear, i still must had done something else wrong whilst at it.

Thanks so much :D

Regards,
Mat

Probably just an incorrectly spelled variable or something minor like that.
Those sort of things always seem to cause the biggest problems :-)

Anyways, glad you got it working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.