Jump to content

PHP Downloader


dafallenangel

Recommended Posts

I downloaded a PHP Downloader script with Counter and I tested it out how they had it setup and for some reason the first page works find but the second page just shows up blank..

 

first page is grabbing the filename and count out of mysql and then echos the filename into a url  download.php?filename=$filename

 

now my register_globals is set to off but that shouldn't effect any since my forum i wrote is sending veribles through url as well.

 

here is the second page

download.php

$download_path = 'download/';

/* set up mysql connection */
include("inc.mysql.php");

if (!empty($file)) {
  /* query database */
  $result = mysql_query('SELECT filename FROM download');

  /* check each row for filename and send file via http header */
  while ($row = mysql_fetch_assoc($result)) {
    if ($row["filename"] == $file) {
      $result = mysql_query("UPDATE download SET downloads=downloads+1 WHERE filename='" . $file . "'");
      header('Location: ' . $download_path . basename($file));
    }
  }
}

 

Thank you

DaFallenAngel

 

 

Link to comment
Share on other sites

Add this to the top of the page

 

ini_set('error_reporting', E_ALL);

 

 

It should show you the errors and give you a good idea of whats wrong. I don't see anything that should echo on screen. The second page is only a "force download" page. I take it that the download is not being forced?

 

Nate

Link to comment
Share on other sites

When I modify it a little and run it, it redirects to the file specified in the header(), it does not force the download because it does not have the proper headers and such.

 

Here is some code I have that DOES force a download. I even tested it in place of yours and it forced the download properly. I integrated it with your code, try that and see what you get. (there might be errors as I did this real quickly and cannot truly test it because I don't have the database information to test against.)

 

<?php

$download_path = 'download/';

/* set up mysql connection */
include("inc.mysql.php");

if (!empty($file)) {
  /* query database */
  $result = mysql_query('SELECT filename FROM download');

  /* check each row for filename and send file via http header */
  while ($row = mysql_fetch_assoc($result)) {
    if ($row["filename"] == $file) {
      $result = mysql_query("UPDATE download SET downloads=downloads+1 WHERE filename='" . $file . "'");

	$filename = $download_path.$file

	// required for IE, otherwise Content-disposition is ignored
	if(ini_get('zlib.output_compression'))
	  ini_set('zlib.output_compression', 'Off');


	// addition by Jorg Weske
	$file_extension = strtolower(substr(strrchr($filename,"."),1));
	switch( $file_extension )
	{
	  case "pdf": $ctype="application/pdf"; break;
	  case "exe": $ctype="application/octet-stream"; break;
	  case "zip": $ctype="application/zip"; break;
	  case "doc": $ctype="application/msword"; break;
	  case "xls": $ctype="application/vnd.ms-excel"; break;
	  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
	  case "gif": $ctype="image/gif"; break;
	  case "png": $ctype="image/png"; break;
	  case "jpeg":
	  case "jpg": $ctype="image/jpg"; break;
	  default: $ctype="application/force-download";
	}


	header("Pragma: public"); // required
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false); // required for certain browsers 
	header("Content-Type: $ctype");
	// change, added quotes to allow spaces in filenames, by Rajkumar Singh
	header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".filesize($filename));
	readfile("$filename");
	exit();
    }
  }
}


?>   

Link to comment
Share on other sites

register_globals should not make a difference. I have them turned off and it works for me....

 

try this... make a file called notes.txt and put something in it... it don't matter what it is, it just needs to have data so you know that it worked....

 

Then run this code

 

<?php
$download_path = '/';
$filename = 'notes.txt';

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');


// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>   

 

It works with register_globals off with my server.... register globals *should* not have any effect on what this script does though I could be wrong.

 

Nate

Link to comment
Share on other sites

register_globals should not make a difference. I have them turned off and it works for me....

 

try this... make a file called notes.txt and put something in it... it don't matter what it is, it just needs to have data so you know that it worked....

 

Then run this code

 

<?php
$download_path = '/';
$filename = 'notes.txt';

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');


// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>   

 

It works with register_globals off with my server.... register globals *should* not have any effect on what this script does though I could be wrong.

 

Nate

 

i wasn't meaning that code i ment the code that i had it wasn't working because i had register_globals set to off

 

and when i tried your code i got errors at

 

if(ini_get('zlib.output_compression'))

  ini_set('zlib.output_compression', 'Off');

Link to comment
Share on other sites

hmmmmm.... when I run this....

 

<?php
$download_path = '/';
$filename = 'notes.txt';

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');


// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>   

 

it works perfectly with no errors.... what errors are you getting?

 

Nate

Link to comment
Share on other sites

<?php

$download_path = 'download/';

/* set up mysql connection */
include("inc.mysql.php");

if (!empty($file)) {
  /* query database */
  $result = mysql_query('SELECT filename FROM download');

  /* check each row for filename and send file via http header */
  while ($row = mysql_fetch_assoc($result)) {
    if ($row["filename"] == $file) {
      $result = mysql_query("UPDATE download SET downloads=downloads+1 WHERE filename='" . $file . "'");

	$filename = $download_path.$file

	// required for IE, otherwise Content-disposition is ignored
	if(ini_get('zlib.output_compression'))
	  ini_set('zlib.output_compression', 'Off');


	// addition by Jorg Weske
	$file_extension = strtolower(substr(strrchr($filename,"."),1));
	switch( $file_extension )
	{
	  case "pdf": $ctype="application/pdf"; break;
	  case "exe": $ctype="application/octet-stream"; break;
	  case "zip": $ctype="application/zip"; break;
	  case "doc": $ctype="application/msword"; break;
	  case "xls": $ctype="application/vnd.ms-excel"; break;
	  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
	  case "gif": $ctype="image/gif"; break;
	  case "png": $ctype="image/png"; break;
	  case "jpeg":
	  case "jpg": $ctype="image/jpg"; break;
	  default: $ctype="application/force-download";
	}


	header("Pragma: public"); // required
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false); // required for certain browsers 
	header("Content-Type: $ctype");
	// change, added quotes to allow spaces in filenames, by Rajkumar Singh
	header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".filesize($filename));
	readfile("$filename");
	exit();
    }
  }
}


?>   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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