Jump to content

Need PHP download script to redirect to main page after downloading file


GrdLock

Recommended Posts

Ok, I have a download script. The script is called like:

 

http://www.site.net/index.php?action=downloadfile&filename=file001.zip&directory=directory12&

 

The download file code is below. My site hosts quite a few files that get linked on other websites. Problem is, when that link is clicked, obviously it just starts the file download, as if it were a direct link to the file.

 

What I need to do, is still have it download the file when the link is clicked, but I would also like it to redirect the browser to my homepage as well.

 

I tried placing this after the download headers but it didn't work.

 

header("Location: http://www.site.net");

 

Can anyone give me an idea how I can get the code to do that? Keep in mind that I'm not fluent in PHP.

 


case 'downloadfile';

	$filename = getGetVar('filename');
	$directory = getGetDir('directory');


	$current_dir = $uploads_folder_name;
	if ($directory != '')
	{ $current_dir.="/$directory"; }


	$filename = basename($filename);


	if (!$grants[$user_status][DOWNLOAD])
	{
		place_header($mess[111]);
		show_Contents();
		break;
	}


	if (!file_exists("$current_dir/$filename"))
	{
		place_header($mess[125]);
		show_Contents();
		break;
	}


	if (!is_path_safe($directory, $filename))
	{
		place_header($mess[111]);
		show_Contents();
		break;
	}


	list($upl_user, $upl_ip, $filestatus, $contents) = get_file_description("$current_dir/$filename", $comment_max_caracters);
	if ($validation_enabled && $filestatus == UNVALIDATED && !$grants[$user_status][VALIDATE])
	{
		place_header($mess[111]);
		show_Contents();
		break;
	}



	$size = filesize("$current_dir/$filename");
	$daily_size = get_daydownload();
	if (($max_daily_download_mb > 0) && (($size + $daily_size) > ($max_daily_download_mb * 1024 * 1024)))
	{
		place_header($mess[212]);
		show_Contents();
		break;
	}


	$monthly_size = get_monthdownload();
	if (($max_monthly_download_mb > 0) && (($size+$monthly_size) > ($max_monthly_download_mb * 1024 * 1024)))
	{
		place_header($mess[213]);
		show_Contents();
		break;
	}
	increasefiledownloadcount("$current_dir/$filename");
	increasebytecountdl("$destination/$userfile_name");
	if (($user_status != ANONYMOUS) && ($logged_user_name != ''))  // Update user statistics
	{
		list($files_uploaded, $files_downloaded, $files_emailed) = load_userstat($logged_user_name);
		$files_downloaded++;
		save_userstat($logged_user_name, $files_uploaded, $files_downloaded, $files_emailed, time());
	}


	header("Content-Type: application/force-download; name=\"$filename\"");
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: $size");
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Expires: 0");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");

	// Decrypt file if encryption enabled
	if ($encrypt_filecontent)
	{ decrypt_file("$current_dir/$filename", true); }
	else
	{ readfile_chunked("$current_dir/$filename"); }


	exit;
	break;

This is kind of tricky so follow closely.

 

You should be building your entire site, and including a redirect to the download.php location.  :P

 

This way the external links point to your index.php which shouldn't switch case to file download, but rather should construct your sites entire main page that includes an http or javascript redirect to another script, based on parameters passed to index.php, that contains the headers for the file download.

 

Phewww..

This is kind of tricky so follow closely.

 

You should be building your entire site, and including a redirect to the download.php location.  :P

 

This way the external links point to your index.php which shouldn't switch case to file download, but rather should construct your sites entire main page that includes an http or javascript redirect to another script, based on parameters passed to index.php, that contains the headers for the file download.

 

Phewww..

 

Ok, thanks for the worthless answer.

 

The site's already built, and I don't want to redo a ton of stuff just for this one thing.

 

Instead of sarcasm, how about a constructive answer? I'm aware it could be done the way you're talking about, I already thought about that. However, doing that would cause every link that's posted anywhere on any other site to break.

 

Can what I'm asking for be done or not?

I don't want to give you any more worthless answers but to answer your question...

 

No, it can not be done. Since you say your site's already done, it wouldn't take too much work to rearrange your logic and add an http redirect instead of what your currently doing. And your external links would not be affected.  :o Really? Really!

Archived

This topic is now archived and is closed to further replies.

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