Question

The bellow code is a page with an IF statement calling the downloadpage.

if ($currentpdt<$updatedpdt)

  echo "updatedpdt is greater than currentpdt.";

else
  echo "updatedpdt is not greater than currentpdt";
  require("dlfile.php");

?>

The above page has other things on it, mostly just SQL connections to define variables. Notice if the IF statement is false the "dlfile.php" file is required. This file is as follows

<?php
// local file that should be send to the client
$local_file = 'dl.zip';
// filename that the user gets as default
$download_file = 'your-download-name.zip';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5; 
if(file_exists($local_file) && is_file($local_file)) {
    // send headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream'); 
    header('Content-Length: '.filesize($local_file));

    header('Content-Disposition: attachment; filename="'.$download_file.'"');

    // flush content
    flush();    
    // open file stream
    $file = fopen($local_file, "r");    
    while(!feof($file)) {

        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));    

        // flush the content to the browser
        flush();

        // sleep one second
        sleep(1);    
    }    

    // close file stream
    fclose($file);}
else {
    die('Error: The file '.$local_file.' does not exist!');
}

?>

When I run the dlfile.php file by itself it works just fine and the download prompt appears as it should. However when it is called by the first file this is what I get.

PKÇ`w?6J@T1popup.jsUT   %DÍN%DÍNUxz{]’ÏNÃ0ÆÏÉSX»¬›ª¶š8 PÅ×Á¤©ÛÚ8ÊŸmíÝIX7MœâXŸþì¤ZzE™`:Ä6vÜÀ/·œ}8?(ÝUæ   zÔh…G©‚£Ò-¡#Êã£Ò_î¦k9kæˆAxøÝÛÚƒ’XÀ{Lj1!P÷¯Ïœ<   éÇ䌱›CÎ8º…™H¡!ü¹DÔbÔŽÑJ´éJÕÍË©äÍ% ,:Îöˆ0xožÊ2awŸ® Û—Q†§ÂæÅÕµ|[rÎÊœ·J÷÷èÿDÎÂÂ4_PÃÊ°µ¦|$)Òp)n•EéÉ*té:¡‹ÊIKc*qõŒ.w>².a$ªoÑŒ˜dGÕú¡~¬ª|@Õ¾~¨ªÕsZM\E'6¿ø+È Î >këeиËu~çt+UÙRÑ‘.~‚u¹g›3?ó_PKÇ`w?ÌmO‹jpreview.phpUT %DÍN%DÍNUxz{­VmoÛ6þlýŠ«ÔöKq›]lÙh“¬/ȶ ñVÛ`Ðe3–I¤ì&AþûîHù-ΧmùDw÷<<ÝÝÃSXÎÊ Ž¥)7R%s1­4³BI0ÜZ!§&ÐüïJh>V2å­fÎyöêÖŒ}p„Ív/ˆæ=·°dZ°IÁ äZ-@Ȳ²àÈèh ƒÄ­£ñûËÑM´5ÿj·a»8ƒf³¥3& dÜÇxî%«Åmûg@È   2n3#ã>Ä[ÓE«ììê‰P:’Òɘå‡ìh|Âî,”½?ÀØû}=S<5ÄA˜ž‚Ðøæ,›ƒfvQ ȸò– ª²ùmûgØ´E3ž¨oc‘ ·®}‚]ûz&ÊÃV•OU¦ÛLÝ\Zn,äJÂ"ª2P*“jQZ°lÊM rh«KeZ4¬ÇÐì{ÿ ‰ôA£áRóéXó²`¨‡0nýY‡µ[Ñ÷Ãv«Æ´c#ÂcoÎ?¼ÁÅåÕåèò‚,D¢f™@ü¦%Êì¼µ–/(…ª¹å©…[¶du†·háYo'Jã?ÀdV³tŽ`(Ôª’f(‹ˆúѽ··a^Њ۸r¸ÞÛJK¤S²á à"Iš¬Ù†µ2P$D7©D‘AÉ4[`6°Vt61Ù‡úb€JÁ‘ªI¨k5³“󋄃Üëˆ(ð%¹ö|0©cOžF¢=!ç:Î¥ø\ 9ç^‡:Í>>«çÞ°RÝž%WâÖÁ¨U u:|mïôo"I¢ÏÓ’+që`×M0=·U _º>9óàD™ÜöÉ›/Ñ“]ØŽ‰ý°Æ$;AkvÞ³äeê¸/q J¯ZC÷$„ˆƒËTeÜÉ«–-Ñþ"Û‘T~ZÇÿ…Ósu¢‡ƒ 迸øõ|ôõú>Œ~¾‚ëßÞ]}<‡°Ç_^ŸÇñÅèÂ;N£“.Œ4“F+âøò—0€ú'œY[žÅñjµŠV¯#¥§ñèsL'Æ…R†G™ÍÂAÐw=,˜œ&!—ÎÀY6}Ú¼4uçÖµ} cƒ§3…×êŒÕ¡¼LB\º–KÛ±w%¡~JBË¿Yw|PCïÆ);o¾7Ò=/ðÂû¿¸…Q7o~ø±ÓÝ4†ôZVØ‚~Â6Áälך/_õcïÁBÈ9h^$¡±wø0ã܆0Ó<¯-ñ„‘v#œC¬"œ(­:ì&xNµªdÖIU¡ôÙwyž÷ð¦×S!qW¿*¿õƒYào*|½œ-Dqw¿s1ÉŽá-~°ÇðKnEŠƒ}ï®EÞqÉ]=Q?.w´_ú›º!®m^øN‚ëër8ØmK}ïῬ–Å.èéµHèß©ú¾;añ–Â7¿z¦Žâš:‹+{üǹŸÿ¦!~¿>ž»Ú}qeñãà?œæ(P^ä‘«k«ºª©b<Ç=»¿ýØÏTß]hÿPKÇ`w?6J@T1 ¤popup.jsUT%DÍNUxPKÇ`w?ÌmO‹j ¤preview.phpUT%DÍNUxPK‰X

I think the browser is trying to read the file as a web document but I am not sure. I have attempted to take out the tags in dlfile.php and I also switch the

header('Content-Length: '.filesize($local_file));

to

header('Content-Disposition: attachment; filename="'.$download_file.'"');

the code still executed fine by itself but generated the same weird output when called for by the original file.

I have tried to just put the code from dlfile.php into the original file and got the same output.

What's wrong?

Was it helpful?

Solution

Actually it's not. Without braces your code is interpreted as:

if ($currentpdt<$updatedpdt) {
    echo "updatedpdt is greater than currentpdt.";
} else {
  echo "updatedpdt is not greater than currentpdt";
}

require("dlfile.php"); //Is required no matter what

Try using braces to increase readability and fix this problem:

if ($currentpdt<$updatedpdt) {
    echo "updatedpdt is greater than currentpdt.";
} else {
  echo "updatedpdt is not greater than currentpdt";
      require("dlfile.php"); //Now just included if the if is false
}

Also, if you echo something from you first file then the second file will not be able to set headers and the file's raw contents will be outputted. So you'll need to remove the echo's from your first file so that only the zip's contents gets outputted.

OTHER TIPS

You have the Dangling else problem, if you don't use brackets, only the line immediately followed is considered in the else block.

http://en.wikipedia.org/wiki/Dangling_else

Aside from the syntax problem that others have correctly commented on, the calling file has echo() calls and the file it is including via require() has header() calls. But header() calls will not work if they follow any output to the page. So you need to get rid of those echo() calls. That may be the source of the wacky output that you're getting.

Well... the echos are the problems. Try adding these two lines at the start of your code, you'll see the problem:

error_reporting(E_ALL);
ini_set('display_errors',1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top