• 0

PHP help: Download a file.


Question

Hey everyone,

I have a php file that connects to the Invision Power Board database and gets the member data. I need this to restrict downloads to certain members. So when they have a certain ID, they will be able to download the file. Otherwise, they will need to purchase the required membership. I will make it available to guests so you can see what is going on. It is just a picture for testing anyway :p

Everything works fine, until I need to do header() commands in order to download the file. I get a bunch of headers are already set errors: Warning: Cannot modify header information - headers already sent by (output started at /home/program/public_html/CheckLogin.php:35).

<?php

/**
 * Edit this path, to where you have your forum installed.
 */
$forum_path = 'forums';
$file = getcwd();
$file = substr($file,0,14);
$file = $file . "downloads/IM2.png";

/**
* We will change directories so that proper directory is picked up
*/
chdir( $forum_path );

/**
* Get some basic IPB files
*/
define( 'IPB_THIS_SCRIPT', 'public' );
require_once( 'initdata.php' );

/**
* Get IPB registry
*/
require_once( IPS_ROOT_PATH . 'sources/base/ipsRegistry.php' );
require_once( IPS_ROOT_PATH . 'sources/base/ipsController.php' );

/**
 * initialise the ipsRegistry
 */
$ipbRegistry    = ipsRegistry::instance();
$ipbRegistry->init();

$memberData = $ipbRegistry->member()->fetchMemberData();
echo $memberData['member_group_id']. "<br />";
switch ($memberData['member_group_id']){
	case 0:
		echo "Not sure";
		break;
	case 1:
		echo "Validating";
		break;
	case 2:
		echo "Guests";
		break;
	case 3:
		echo "Member";
		break;
	case 4:
		echo "Root Admin";
		break;
	case 5:
		echo "Not sure";
		break;
	case 6:
		echo "Administrator";
		break;
	case 7:
		echo "Global Moderators";
		break;
	case 8:
		echo "Advanced Member";
		break;
	case 9:
		echo "Not sure";
		break;
	case 10:
		echo "Not sure";
		break;
	case 11:
		echo "Premium Member";
		break;
	case 12:
		echo "Phoenix Member";
		break;
	case 13:
		echo "Not sure";
		break;
	case 14:
		echo "Not sure";
		break;
	case 15:
		echo "Not sure";
		break;
	case 16:
		echo "Not sure";
		break;
	case 17:
		echo "Not sure";
		break;
	case 19:
		echo "Not sure";
		break;
}

if($memberData['member_group_id'] == 2)
{
	header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
else
{
	echo "<br /> Not valid";	
}

?>

Line 35: $memberData = $ipbRegistry->member()->fetchMemberData();

I NEED this line to connect to the database. The header lines are from php.net, so it should work fine.

Live page - http://www.phstudios.com/CheckLogin.php

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

I'm pretty sure that your problem is not with line...

$memberData = $ipbRegistry->member()->fetchMemberData();

But with the line below it...

echo $memberData['member_group_id']. "<br />";

Comment that line out.

The error is basically telling you something was already output to the page. You need to make sure nothing is. Not even whitespace before your initial <?php tag. Header information must always come first.

Link to comment
Share on other sites

  • 0

Okay commented it out and its still doing the same thing. You can tell I changed it since it will not display 2 as it did before. So maybe it is that line?

Edit: I commented out the switch statement and it DOES download. I cannot echo until after I change the headers?

Link to comment
Share on other sites

  • 0

Yes, good job on the switch statement. I didn't read the script thoroughly but you got the right idea, you just have to prevent it from outputting anything before the header. It is working fine now mate. The errors you are seeing is the cached erroneous page you had before. Close the page and re-open it, and you will see no errors.

Link to comment
Share on other sites

  • 0

Too bad you didn't get a chance to nab iClient from invisiontweaks before they shut down. That would have been perfect for you as you'd be able to organize downloads for guests and usergroups.

Otherwise the IPB Download Mgr would be what you'd use, but you seem to be fine w/out an add-on. :)

Link to comment
Share on other sites

  • 0

Some of the downloads I provide are above 3GB. The IP Download Manager has the upload size limit. Plus I want a nice "exclusive" page that only they can have access to.

Link to comment
Share on other sites

  • 0

By the way, mate, I am on a crusade to stop anybody reading these forums from using repetitive code. If your code is repetitive, rest assured it is inefficient. I hate, hate, hate inefficient code.

These two lines...

$groups = array(1 =&gt; 'Validating', 'Guests', 'Member', 'Root Admin', 6 =&gt; 'Administrator', 'Global Moderators', 'Advanced Member', 11 =&gt; 'Premium Member', 'Phoenix Member');
echo (array_key_exists($memberData['member_group_id'], $groups)) ? $groups[$memberData['member_group_id']] : 'Not sure';

Eliminate all this junk...

switch ($memberData['member_group_id']){
 case 0:
 echo "Not sure";
 break;
 case 1:
 echo "Validating";
 break;
 case 2:
 echo "Guests";
 break;
 case 3:
 echo "Member";
 break;
 case 4:
 echo "Root Admin";
 break;
 case 5:
 echo "Not sure";
 break;
 case 6:
 echo "Administrator";
 break;
 case 7:
 echo "Global Moderators";
 break;
 case 8:
 echo "Advanced Member";
 break;
 case 9:
 echo "Not sure";
 break;
 case 10:
 echo "Not sure";
 break;
 case 11:
 echo "Premium Member";
 break;
 case 12:
 echo "Phoenix Member";
 break;
 case 13:
 echo "Not sure";
 break;
 case 14:
 echo "Not sure";
 break;
 case 15:
 echo "Not sure";
 break;
 case 16:
 echo "Not sure";
 break;
 case 17:
 echo "Not sure";
 break;
 case 19:
 echo "Not sure";
 break;
}

Link to comment
Share on other sites

  • 0

Not sure what version of IP Download Manager you've used but while you can actually set the upload limit (IP Download Mgr > Categories > <<Category>> Edit Category) the actual upload limit itself is dependent on your host alone. If their upload limit is 16MB then you'd have to add a php.ini (or .htaccess or different depending on their configuration) to change that upload limit so you could use more than what's set server wide.

You can see here that the upload limit on my server is a default 16MB so either I change it globally since I have root access or I can create my own php.ini to include

upload_max_filesize = 20M
post_max_size = 30M

OR if you have to use htaccess

php_value upload_max_filesize 20M
php_value post_max_size 30M

560397951.png

Link to comment
Share on other sites

  • 0

By the way, mate, I am on a crusade to stop anybody reading these forums from using repetitive code. If your code is repetitive, rest assured it is inefficient. I hate, hate, hate inefficient code.

These two lines...

$groups = array(1 =&gt; 'Validating', 'Guests', 'Member', 'Root Admin', 6 =&gt; 'Administrator', 'Global Moderators', 'Advanced Member', 11 =&gt; 'Premium Member', 'Phoenix Member');
echo (array_key_exists($memberData['member_group_id'], $groups)) ? $groups[$memberData['member_group_id']] : 'Not sure';

Eliminate all this junk...

snip

I know, the switch was just used to determine what number goes with what group. I have removed the switch from the file already since I know what IDs I need.

Not sure what version of IP Download Manager you've used but while you can actually set the upload limit (IP Download Mgr > Categories > <<Category>> Edit Category) the actual upload limit itself is dependent on your host alone. If their upload limit is 16MB then you'd have to add a php.ini (or .htaccess or different depending on their configuration) to change that upload limit so you could use more than what's set server wide.

You can see here that the upload limit on my server is a default 16MB so either I change it globally since I have root access or I can create my own php.ini to include

upload_max_filesize = 20M
post_max_size = 30M

OR if you have to use htaccess

php_value upload_max_filesize 20M
php_value post_max_size 30M

snip

Yeah I tried that a while ago. I could only go up to 50MB or so, not 3GB. I would try more things, but like I said, I wanted an exclusive page for only specific members (not specific forums, but a whole new section).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.