PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I think I've seen this topic some here before but I can't seem to find it anywhere.
Is it possible to either keep track of the progress of a download or find out when a download has been completed using PHP?
For example a user opens a new account and is given a free gift which they can download, but once they have downloaded the file then this opion is removed from their account.
I also need to make sure that the free-gift-download-option isn't removed if the download fails to complete (ie the connection is terminated during the download)... simply updating the users account as soon as the download begins isn't an option.
I know this can be done, but I'm not sure if it can be done with PHP
If anyone has any pointers, has done this before, or has any theories then I will be a happy Bunny
php is server-side, sounds like a client-side operation to me
yep u r rite but there sholud b some way that will inform server side i.e PHP either client has downloaded complete file or not due to any reason connection lost etc
dull1554 wrote:php is server-side, sounds like a client-side operation to me
It can be done server-side. They use a similar way of working at http://www.bleep.com which will remove the option of downloading purchased MP3 music after the user has downloaded it.
There should be a way to do it - but ill have to have a good think about it. All i can think of at the moment is getting javascript to redirect the user to a second page after the download is finished - then that page runs the code to say the download is done.
Another way would be to have the use click like a next button when the download is done.
But if the user has bought the download, then shouldnt they be allowed to download it as many times as they like - incase they delete it or something?
Okay, first, this is purely a theory solution. I have not tested any code on this. However, I have a very strong belief that it will work as stated.
Basically, instead of directly offering the download, you provide the download via PHP.
Say you have this: download.php?file=123
PHP would open up whatever file 123 equalled, and would write it out to the browser with the proper header information and what not.
This way, you can keep the file stored on the server where ever you want, and use PHP to keep track of what is and what isn't downloaded, and how many times.
Yep, I'm doing the PHP download thing at the moment (ie download.php?file=someFile) and it's easy enough to update MySQL when the download first starts... but at the moment I can't work out how to let PHP know when the file actually finishes downloading... which is what I need.
I also need to make sure that any 'broken connections' half way though a download don't result in PHP thinking the file has been downloaded.
Hope that makes sense
In the back of my mind I'm thinking that the server must know when the file is being accessed, and how much information has been sent (which is how band-width is monitored) but wether or not this is something PHP can handle is a mystery.
It's been suggested that I look at PHP's FTP functions, but I'm not sure using FTP is the answer. I'll take a look at them anyway.
I seem to remember that if your using something like readfile() to throw out the file, then placing any download processing after the readfile() function will do what you want.
If I remember correctly, if the connection is lost or the download is cancelled then anything after the readfile() function doesn't get processed.
redmonkey wrote:I seem to remember that if your using something like readfile() to throw out the file, then placing any download processing after the readfile() function will do what you want.
If I remember correctly, if the connection is lost or the download is cancelled then anything after the readfile() function doesn't get processed.
<?php
// Various bits of code and headers go here
readfile("fileToDownload.mp3");
// Anything past this point will be processed after the file has downloaded
echo "YOUR FILE HAS BEEN DOWNLOADED";
?>
...you've got me thinking now. I'm off to give it a try
The basis of this was that a temp file was created and then thrown out to the user via readfile() then the temp file was deleted. If the file download did not complete because the connection was lost or the user pressed cancel then the temp file was not deleted. I could then check if the user had downloaded the file by checking the existance of the temp file (the name of which was stored against a user in a DB).
Or at least, that seems to 'ring a bell' as to how I did it before.
Here's a little test script I put together to see if this works.. and it does. Many thanks to redmonkey for the pointer towards readfile().
The text file in the following script will ONLY be written when the file has downloaded... if the download is canceled or the connection breaks then no file is written.
From here you could replace the text file creation with MySQL stuff.
<?php
$file = "test.zip"; // The name of the file to download
$fileSize = filesize($file); // Get the zip's file size
header("Content-Type: application/x-zip-compressed");
$readFileSize = readfile($file); // Start reading the file. Bytes read are returned.
$fo = fopen("stream_result.txt", "wb");
fwrite($fo, "file:{$file}, size:{$fileSize}, downloaded:{$readFileSize}");
fclose($fo);
?>
The text file output is...
file:test.zip, size:398589, downloaded:398589
This code was great.... except for not being able to determine if a user has cancelled the download. There's no way to estimate how long the download should take before going to the txt file that was/wasn't written and checking to see if it was complete.
Anyone know of any other scripts (I am assuming it will have to be javascript) that can check to see if a file download connection is still alive?
My goal is to create a script that will put users in a queue to limit the number of simultaneous downloads. When a file is complete or cancelled, the next user in line will be allowed to start their download.
I have begun to realize that php isn't going to be able to check for a live status of a file so I am looking for other solutions.
It a combination from several sources, including user notes over at php.net. I've never had occasion to use it, but its been sitting in my library folder as a means to monitor download completion.