FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Automatic download start !!





salman_500
hey,

you see i have this download thing on my site.... and i programmed it so that once some1 clicks on the download link... he is sent to a page where a script runs and adds a +1 to the "downloaded_times" field in the table..... now i want it so that the people dont have to see this page...

when they click on the link... they are redirected to the page...but before it can be loaded....a download dialog box pops up ... and the page redirects to the previous page.... also :

its a template download option.... and the right download has to be downloaded ( Duhh ! Razz ) so i tried using this script... that would get the id of the download page... and match that id with the database... get the right record.... and then start the download (this part i want done... manual atm) update the record and then go to previous page ( this part to i wana know Razz ).... but all i seem to get is a blank page...i use this script.... a bit messy though.....

oh yea... thae pages are in the switch command... so two different cases are there with the same script for gettinh the right record....


Code:
case 'download':

$tempdown1 = "select * from templates where id = '$tempid'";
$tempdown2 = mysql_query($tempdown1) or die(mysql_error());
while ($tempdown = mysql_fetch_array($tempdown2)) {

$tempid = $_GET['id'];
$timedownloaded = (($tempdown['downloaded_times']) + 1);
mysql_query("UPDATE templates SET downloaded_times = '$timedownloaded' WHERE id = $tempid") or die (mysql_error());

echo "Click the link below to start your download :<br><br><a href=\"modules/templates/files/". $tempdown['download_link'] ."\">Click Here</a>";

}
mysql_free_result($tempdown2);
break;


is anything wrong here.....

the sript in the other case is pretty much same... so is that the reson for nothing being shown????

thnx !
kv
On clicking the link, you don't have to necessarily "go" to onother page and "return" to previous page on the browser. You can write your php download script in such a way, it updates the db and starts download automatically.

For ex, let us say the url is something like this

http://whatever..../download.php?id=1

in download.php,


Code:

<?
$id=$_REQUEST['id'];
/*
update your db here */

$filename=getFileName($id); //get the file name to be downloaded using id

readfile($filename);

?>


As long as you don't echo anything in your download code, it works -- update and download without even redirecting. You can echo error in case of error in sql and provide a 'back' link.
salman_500
0i know it sounds kinda stupid to do that... but i tried... it aint possible ( for me atleast) because its already a php page.... with the script in it.... so i could make it work that way....

but the code you provided was actually the thing i wanted... Razz

but about this part...



Code:
$filename=getFileName($id);


the name of the download file ... how will it get it if its a .zip, .rar etc...

the field that contains this info in the database is "download_link"..

can you also plz tell me how to redirect once the download has begun???

thnx !
salman_500
hey...

i still need help here..

plz !
salman_500
plz any1 !!!!
salman_500
hey... sorry for continous posting.....but...


when i used readfile function..... it opened up the .rar file and started to show this :

Quote:
Rar!ϐs Bt@?43 Alienation\! Read Me !.txtp T s/˵_  X774iC 4[kDY$Ήm)wIͨa}ʓDΔ0{|D~'pH ؁GlvFFvhzVW<>_NN~ͻ=$p#GNѻ8r1c&>"&ԣ36t=:1\]jx;


i couldnt post all of it coz it would have been unfair... just multiply this text into 100 and youll get the amount of data i got... Razz ..

so what is the actuall command of starting a download???

thx !
Fire Boar
Okay, you'll need to set the headers first. Add this to anywhere on your page BEFORE any HTML output takes place.

Code:
$downloadurl = "modules/templates/files/".$_GET['url'];
$error = '<color=#FF0000>Cannot find file</color>';
if ($url != "")
{
 header('Content-Description: File Transfer');
           header('Content-Type: application/download');
           header("Content-Disposition: attachment; filename=\"".basename($url)."\";");
           header('Content-Length: ' . filesize($url));
@readfile($url) OR die($error);
}


That should get you your file in the form of an application, meaning it will download rather than read (since browsers cannot handle applications).

Then append the snippet you posted to this:
Code:
case 'download':

$tempdown1 = "select * from templates where id = '$tempid'";
$tempdown2 = mysql_query($tempdown1) or die(mysql_error());
while ($tempdown = mysql_fetch_array($tempdown2)) {

$tempid = $_GET['id'];
$timedownloaded = (($tempdown['downloaded_times']) + 1);
mysql_query("UPDATE templates SET downloaded_times = '$timedownloaded' WHERE id = $tempid") or die (mysql_error());

echo "Click the link below to start your download :<br><br><a href=\"thisfile.php?id=".$tempid."&url=". $tempdown['download_link'] ."\">Click Here</a>";

}
mysql_free_result($tempdown2);
break;


That should do the trick. Hopefully. I haven't tested or compiled it, but with luck it should work. Oh, and change thisfile.php to the url of your file.
hexkid
salman_500 wrote:
when i used readfile function..... it opened up the .rar file and started to show this :

Quote:
Rar!ϐs Bt@?43 Alienation\! Read Me !.txtp T s/˵_  X774iC 4[kDY$Ήm)wIͨa}ʓDΔ0{|D~'pH ؁GlvFFvhzVW<>_NN~ͻ=$p#GNѻ8r1c&>"&ԣ36t=:1\]jx;


so what is the actuall command of starting a download???


You have to let the browser know what type of stuff you're sending. In the absence of such information, most browsers will try to interpret the data as HTML.

Use the header() function and tell the client what you are sending
Code:
# header('Content-Type: text/html'); // for HTML
# header('Content-Type: text/plain'); // for plain text
# header('Content-Type: text/javascript'); // for javascript code
# ...

I don't know the type for RAR files, maybe you can fish it out of google.
Or try a 'Content-Type: application/binary' content type.

VERY IMPORTANT: The Content-Type header should have a character set identifying the character set used for the data.
Code:
header('Content-Type: text/plain; charset=us-ascii');
Alie
I'm not sure if I've understand your request correctly....
If you want to add something into your database and in the same time make direct link to the file maybe you'll use redirection?

Code:
$result = MySQL command
Header("location: your_direct_url");
salman_500
hey...

this is what i tried:

Code:
header('Content-Type: text/x-rar-compressed; charset=us-ascii');
$filename = "modules/templates/file/".$tempsdetail['file'];
readfile($filename);


but when i ran it i got this...

Quote:

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 127
Rar!ϐs Bt@?43 Alienation\! Read Me !.txtp T s/˵_  X774iC 4[kDY$Ήm)wIͨa}ʓDΔ0{|D~'pH ؁GlvFFvhzVW<>_NN~ͻ=$p#GNѻ8r1c&>"&ԣ36t=:1\]jx; W"ߧ9]zBW w&8M<NX Nl@S\w]ӳV]'K_1{1PX1'ф.........................


and line 15 on modules.php is :

Code:
<link href="Templates/Urbane/CSS/css.css" rel="stylesheet" type="text/css">


line 127 on templates.php is :

Code:
header('Content-Type: text/x-rar-compressed; charset=us-ascii');


any ideas??? Embarassed Confused

@ Alie

yea that is also what i wanted... but i get the lame header cannot be sent error...so i cant use it Sad
lordfrikk
Firstly and definitely, RAR's contect type isn't this:

Code:
text/x-rar-compressed


but this:

Code:
application/x-rar-compressed


When trying to make auto-download script, you should use most of the time this:

Code:
header('Content-Type: application/octet-stream');


Next thing, you only use charset=xxx part when the content-type is text/something... you really can't have charsets with RAR files Laughing Laughing

Anyway, thing you want to accomplish is called Forced Download... I found quickly one example of how to do it:


Code:
<?php

$filename = $_GET['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));

if( $filename == "" )
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) )
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
  exit;
};
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();

?>


BTW, if you are sure you'll be for example using the script to download EXE, ZIP and PDF, all others extensions aren't neccessary...

P.S.: If you want to find what you should write to content-type for a certain extension, fire up google and write sumtin like this:

Code:
rar mime


or

Code:
rar mime type


Where rar is obviously the extension you're looking for.
salman_500
i appreciate the time you took to write all this... but what can you say about this:

Code:
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 131

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 132

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 133

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 134

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 135

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 137

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 138

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules.php:15) in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\templates\templates.php on line 139
sonam
I have not time to type long chunk of code but I am create one script for downloading different items based on id. It is based on flat file. This code you can find and download on my site. Script name is "Multi_download_counter" and just go in and see, maybe you will find something for you.

Sonam
lordfrikk
salman_500 wrote:
i appreciate the time you took to write all this... but what can you say about this ...


What can I say? It's hard to expain something like forced download to someone, who can't even handle "Headers already sent" error... Really, man, you should improve your PHP generally before going into advanced stuff... Unless I write you all the code as it should be on your pages and where should it be, I can solve every error that'll your bad programming causes to the end of the world.

To the point: if PHP says "Headers already sent", it means that there is some text (or even just a simple whitespace) that made you script crash. The download script itself must be in separate file or you have to make sure there is not any output before HEADER command.

Cheers
salman_500
lordfrikk wrote:
salman_500 wrote:
i appreciate the time you took to write all this... but what can you say about this ...


What can I say? It's hard to expain something like forced download to someone, who can't even handle "Headers already sent" error... Really, man, you should improve your PHP generally before going into advanced stuff... Unless I write you all the code as it should be on your pages and where should it be, I can solve every error that'll your bad programming causes to the end of the world.

To the point: if PHP says "Headers already sent", it means that there is some text (or even just a simple whitespace) that made you script crash. The download script itself must be in separate file or you have to make sure there is not any output before HEADER command.

Cheers



well first of all i'd like to tell you that i am new to php....i mention almost everytime that im new to this stuff and im learning.... and i'm 100% sure that you yourself werent born a php programmer ! ... you learn like this dude.... i did not ask you to comment on my skills.... if you wana help me... ill appreciate it and think high of you... if not i dont even bother about you..... its just simple.... everyone starts as a noob..... and so have you.... think of you at my stage..... and probably you'll understand....

Its what life is !

-------------------------------------------------------------------------------
lordfrikk
salman_500 wrote:
lordfrikk wrote:
salman_500 wrote:
i appreciate the time you took to write all this... but what can you say about this ...


What can I say? It's hard to expain something like forced download to someone, who can't even handle "Headers already sent" error... Really, man, you should improve your PHP generally before going into advanced stuff... Unless I write you all the code as it should be on your pages and where should it be, I can solve every error that'll your bad programming causes to the end of the world.

To the point: if PHP says "Headers already sent", it means that there is some text (or even just a simple whitespace) that made you script crash. The download script itself must be in separate file or you have to make sure there is not any output before HEADER command.

Cheers



well first of all i'd like to tell you that i am new to php....i mention almost everytime that im new to this stuff and im learning.... and i'm 100% sure that you yourself werent born a php programmer ! ... you learn like this dude.... i did not ask you to comment on my skills.... if you wana help me... ill appreciate it and think high of you... if not i dont even bother about you..... its just simple.... everyone starts as a noob..... and so have you.... think of you at my stage..... and probably you'll understand....

Its what life is !

-------------------------------------------------------------------------------


Where is the part in which I say you totally suck and are lost? I didn't mean any offence... take it as a GOOD advice. Sure I've been a beginner once, but I weren't trying to do this complicated script without knowing the basics... That was the point of what I have written. If you yell at every1 who tries to help, then you're staying at that level 4ever unless you are really a genius. Not your case, as well as not my.

I learned basics in about month, then were doing myself all the stuff that is so basic like polls, login scripts, sending mails, quizes etc.

Well, I'd like to help you with your "Headers already sent" problem, but if I am going to, you'll learn nothing and just come over again with another error that you "cannot" solve.
Fire Boar
*points to my post* Ah-HEM. *cough* use that *cough*

The errors are there because header information has already been sent. You have to put the header('...') code RIGHT AT THE TOP, BEFORE ANY OTHER INFORMATION HAS BEEN SENT TO THE BROWSER!
lordfrikk
Fire Boar wrote:
*points to my post* Ah-HEM. *cough* use that *cough*

The errors are there because header information has already been sent. You have to put the header('...') code RIGHT AT THE TOP, BEFORE ANY OTHER INFORMATION HAS BEEN SENT TO THE BROWSER!


Not true. You can have header command at the very bottom of the script and it will work the same provided there is no output before it.
salman_500
hey....

im sorry that i did that.... i felt kinda offended..... by the way you said stuff.... so its probably wrong at your end as much as mine.....

well about the error im facing... i didnt know it was that complex becaus i have been solving this problem before too but have exhausted all tricks on this part but it still fails.... well ill tell you that its not been a long time me being in php ....and im pretty much clear about basics.... i started off by modifiying exisiting php-nuke modules.... pure php...that was when i was clueless about it.... but i managed to do BIG stuf.... well forget that...

the error im getting is from nothing else in the script... its only this part in the modules.php file :

Code:
<link href="Templates/Urbane/CSS/css.css" rel="stylesheet" type="text/css">


i tried to run the page seprately.... like not included anywhere... that way i got rid of headers sent error.... but the code you provided....even that did not send up the file for download but simply showed it up strage text mentioned above....

so how can i fix the link to css thingy...coz that seems to be messin up with headers error......

the script around it is this:

Code:
<html><!-- InstanceBegin template="/Templates/urbane.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>

<title>SoloThemes - The Best Free And Paid Web Templates ! </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="Templates/Urbane/CSS/css.css" rel="stylesheet" type="text/css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">


thnx !
salman_500
welll nvm.....

i just found out a person who has helped me solve this problem...

his name is jabapyth.... and this is what he told me to do...

Code:
$filename = "modules/templates/file/".$tempsdetail['file'];
echo "<body onload=\"document.location.href='". $filename ."'\">";


and it works fanatstically !@!@! Razz
lordfrikk
^^^

That's what I was talkin about. And you're at the same level you've been before this problem because you let somebody else solve it for you. Rolling Eyes
urologist
I am very confused,I must learn PHP and Mysql more further!!!
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.