note 57861 added to function.readfile

From: Date: Mon, 17 Oct 2005 06:44:38 +0000
Subject: note 57861 added to function.readfile
Groups: php.notes 
Request: Send a blank email to php-notes+get-97103@lists.php.net to get a copy of this message
Using pieces of the forced download script, adding in MySQL database functions, and hiding the file location for security was what we needed for downloading wmv files from our members creations without prompting Media player as well as secure the file itself and use only database queries. Something to the effect below, very customizable for private access, remote files, and keeping order of your online media. <? # setup SQL statement $sql = " SELECT id, fileurl, filename, filesize FROM ibf_movies WHERE id=' $_GET[id]' "; # execute SQL statement $res = mysql_query($sql); # display results while ($row = mysql_fetch_array($res)) { $fileurl = $row['fileurl']; $filename= $row['filename']; $filesize= $row['filesize']; $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."); } $donwloaded = "downloads + 1"; if ($_GET["hit"]) { mysql_query("UPDATE ibf_movies SET downloads = $donwloaded WHERE id=' $_GET[id]'"); } ?> While at it I added into download.php a hit (download) counter. Of course you need to setup the DB, table, and columns. Email me for Full setup// Session marker is also a security/logging option Used in the context of linking: http://www.yourdomain.com/download.php?id=xx&hit=1 ---- Manual Page -- http://www.php.net/manual/en/function.readfile.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57861 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57861&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57861&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97103) next »