Select "print" from your browser's "File" menu.

Back to Post


Username Post: php Help
Coynster
FusionBB Enthusiast
Posts 402
Coynster
04-17-06 07:39 PM - Post#32848    

OK, I've posted this to some users on here, but I still don't have it resolved, so maybe if a few others look at it, we can get this figured out.

All I originally wanted to do was count how many times my videos were being downloaded.

I'm missing something here and I don't know what it is... I've just started picking up php, so my experience is amateurish at best.

I'm just trying to set up a forced download page that counts downloads and prompts to either Open (stream) or Save As. Currently, when anyone tries to Stream, they get a corrupt file prompt in Windows Media Player using IE. FF downloads the file entirely and then launches Windows Media Player and works fine...

I've hit up php.net, I still can't get it. I would greatly appreciate any help that might get me working properly.

If you guys have ANY other options on how I can do this, I'd greatly appreciate it. I'm about to put up a new video, and I'd really like to get an accurate count as to how many downloads it gets, without users reporting errors all over the place.

The link is: http://www.ecoyne.com/ecoyne_vids.php

The code I'm using is this:
Code:

# Protect Script against SQL-Injections
$id = intval($_GET[id]);
# setup SQL statement
$sql = "SELECT * FROM tblVideos WHERE ID='$id'";

# execute SQL statement
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

# display results
while ($row = mysql_fetch_array($result)) {
$filename= $row['FileName'];
$filesize= $row['Size'];
$fileurl = 'http://www.ecoyne.com/videos/' .$filename;
$counter = $row['Counter'];

// Add 1 to the counter value from the array in $row
$counter += 1;

$sql2 ="UPDATE tblVideos SET Counter='$counter' WHERE ID = '$id'";
$result2 = mysql_query($sql2) or die('Query failed. ' . mysql_error());

$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch ($file_extension) {
case "wmv": $ctype="video/x-ms-wmv"; break;
default: $ctype="application/force-download";
}

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: video/x-ms-wmv");
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$fileurl") or die("File not found.");

}



//Close sql Connection
mysql_close($conn);

?>



Beta site: http://www.ecoyne.com/forum

Coynster
FusionBB Enthusiast
Posts 402
Coynster
04-23-06 10:01 AM - Post#32986    

Anybody? How would you handle the coding of wanting to know how many clicks a video was downloaded?

Beta site: http://www.ecoyne.com/forum

Monkeyra
Chief Internet Man of Europe
Posts 2026
Monkeyra
04-23-06 12:00 PM - Post#32988    

Have no idea how, but I have seen php scripts on the web if thats any help?

www.fordmondeo.org



Coynster
FusionBB Enthusiast
Posts 402
Coynster
04-23-06 12:14 PM - Post#32990    

  • Monkeyra Said:
Have no idea how, but I have seen php scripts on the web if thats any help?


I wish it did - I've done a lot of searching but still haven't got it working just right. What started out as a "simple" task has become quite the struggle... haha

Beta site: http://www.ecoyne.com/forum

greg
FusionBB Fanatic
Posts 4956
greg
04-23-06 02:00 PM - Post#32992    

Try this one. I use it to count clicks to special pages, etc and it works quite well. I don't know how well it would work for you, but I believes it's also for counting downloads.
Click/download counter


The Gunfighters Corral

Tombstone Pictures

Coynster
FusionBB Enthusiast
Posts 402
Coynster
04-23-06 02:40 PM - Post#32996    

See the problem with that counter is it will automatically start playing my videos upon clicking the link, which could be 10MB. I'm trying to find a way to count clicks and still have the ability to Save Target As or Save Link As, as data transfers much quicker that way.

Great simple link for other uses though that I'll definitely take advantage of. Thank you!

Beta site: http://www.ecoyne.com/forum

greg
FusionBB Fanatic
Posts 4956
greg
04-23-06 02:58 PM - Post#32997    

Ok, I wasn't sure if it would work for you or not. I only use it to count hits to other pages and not files.


The Gunfighters Corral

Tombstone Pictures

Nikos
FusionBB Addict
Posts 846
Nikos
06-02-06 09:52 AM - Post#34200    

Ok here is the deal.

For starters the php.ini file has to have output buffering set to anything other than off. If that is not the case the code will not work and you will see various weird results regarding what you need to do.

I would suggest you create a page which will receive a file id (via get) and using that it will connect to the database and increase the file download counter by one (what you need really).

Following that you can have the following code to use the Save As button:

Code:

<?php

$_FileID = retrieveData('g','fid');
$_Action = retrieveData('g','action');

if (!is_numeric($_FileID))
{
dieHere(true,'No File ID passed');
}

if ($_Action == 'download')
{
// Connect to the database and update Counters
$sql = "UPDATE xxx SET field = field + 1 WHERE file_id = '$_FileID'";
// Execute the query here etc.
// ----------------

// Get the file name
$sql = "SELECT filename FROM xxxx WHERE file_id = '$_FileID'";
// Execute the query here etc.
// ----------------
list($_FileName) = ....

// Download the file for the user
$_Extension = strtolower(substr(strrchr($_FileName, '.'), 1));


header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='. basename($_FileName) . ';' );
header('Content-Transfer-Encoding: binary');
header('Content-Length: '. @filesize($_FileName));

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Downloader</title>
</head>
<body>
Some links will go here to call the same php document with a get variable regarding the action (action/download)
</body>
</html>


Contact Me




FusionBB™ Version 3.2 | ©2003-2013 InteractivePHP, Inc.
Execution time: 0.027 seconds.   Total Queries: 46   Zlib Compression is on.
All times are (GMT-4). Current time is 10:45 PM
Top