download completed

Codes here !

Moderators: macek, egami, gesf

gerhardt
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Jul 16, 2003 10:49 pm

download completed

Postby gerhardt » Wed Jul 16, 2003 10:54 pm

Hey,
I have a problem with the following topic. I have a download link on my page. When somebody presses this link the download starts. Now I want to write the date and the name of the file into my data base after the dowload is completed. Does anyone has an idea how this could work? Thanx very muc

YOGI

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Thu Jul 17, 2003 7:11 am

Interesting question. Without getting into low-level apache stuff, there's no perfect way to do this. The only way I can imagine is this: Write a script that uses header() to masquerade as the filetype of the file being downloaded. Then make the script pass the contents of the file to the browser. And once it's done doing so, perform your logging functions. Essentially, as far as the browser is concerned, the PHP script is the file. This will work, but there are at least two important caveats:

First, the filename of the downloaded file will be the filename of the PHP script, not the original file. There's a slick way around this, though: Instead of sending the user to, say /script.php?filename.zip, send them to /script.php/filename.zip .. now, without some server tweaking, this will probably be logged by your server as a 404, but (at least in Apache), script.php will still be processed and you can get the name of the requested file by parsing $_SERVER['REQUEST_URI'], and the browser will assume the name of the file being downloaded is filename.zip. Clever, no?

Second, it's unreliable. Depending on your server and PHP settings, the transfer might time out. Also, if they user is using any sort of download manager, all hell might break loose unless you're far cleverer at scripting than I. You could probably just eliminate download managers entirely by creating the script such that a single IP may only do one download at a time (download managers work by initiating several simultaneous downloads of different sections of the same file).

Well, if you get it working, I'd like to see it in action, especially if you manage to elude the above pitfalls. Good luck.

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Fri Jul 18, 2003 12:46 am

I don't know if this is a good idea....
I can't get it to work, but maybe someone else can follow:


Code: Select all

<?php
$file_name = "ming-0_2a.zip";
$file_size = filesize ($file_name);

if ($fopened = fopen ($file_name, "r")){

   header("Pragma: no-cache");
   header("Cache-Control: no-cache, must-revalidate");
   header("Content-type: application/octet-stream");
   header("Content-length: $file_size");
   header("Content-Disposition: attachment; filename='".$file_name."'");
   
   while(!feof($fopened)) {
      $position = fread($fopened, 1024*20); //20 kbytes/sec
      sleep(1); // wait a second      
   }
   fclose ($fopened);
   echo "Do your database update or whatever, the download has finnished";
   exit;
}
?>


Return to “mySQL & php coding”

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron