|
Posted by Pedro Graca on 11/14/06 22:17
matt wrote:
> I am trying to display a list of files
> in a directory, then send an email to the admin when one is downloaded.
I wrote this set of functions, just for you :)
<?php
/* ******************************************************
This code is in the public domain.
Feel free to use and adapt to your needs.
*** NO GUARANTEES ***
name: file_download.inc.php
version: 20061114
Author: Pedro Graca
****************************************************** */
######## download_id_is_valid($id, $filelist)
# verifies if the $filelist array has an element
# with the specified $id for index
#
# Returns true or false
#
function download_id_is_valid($id, $filelist) {
if (!$filelist) return false;
if (!isset($filelist[$id])) return false;
return true;
}
######## download_file_is_valid($name)
# verifies if the $name is valid for download
#
# Returns true or false
#
function download_file_is_valid($name) {
clearstatcache();
if (!is_file($name)) return false;
return preg_match('/(?:jpg|png|jpeg|gif|bmp)$/', $name);
}
######## file_list($d)
# builds an array with all valid files in the
# $d directory
#
# Returns the array with the file names
# or false if no valid files found
# Also returns false if the directory could not be read
#
function file_list($d) {
clearstatcache();
if (!is_dir($d)) return false;
$dh = opendir($d);
if (!$dh) return false;
$retval = array();
while (($f = readdir($dh)) !== false) {
if (download_file_is_valid($d . '/' . $f)) {
$retval[] = $f;
}
}
return (count($retval) ? $retval : false);
}
######## send_file_by_id($id, $filelist, $d)
# sends the file $filelist[$id] from the directory $d
# to the client
#
# Returns false if the file to send is invalid
#
function send_file_by_id($id, $filelist, $d) {
if (!download_id_is_valid($id, $filelist)) return false;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' .
rawurlencode($filelist[$id]) .
'"');
readfile($d . '/' . $filelist[$id]);
return true;
}
?>
And I'd use like this:
<?php
require 'file_download.inc.php';
### some constants
define('DOWNLOAD_DIRECTORY', '.');
define('EMAIL_ADMIN', 'admin@example.com');
# if the directory contents change between requests
# the file sent to the client and the file requested may
# be different. To avoid that, the file list is saved
# in a session variable.
session_start();
if (!isset($_SESSION['files'])) {
$_SESSION['files'] = file_list(DOWNLOAD_DIRECTORY);
}
# If the request is for a valid file, send the
# file (and mail the admin) and exit.
if (isset($_GET['id']) && download_id_is_valid($_GET['id'], $_SESSION['files'])) {
if (send_file_by_id($_GET['id'], $_SESSION['files'], DOWNLOAD_DIRECTORY)) {
mail(EMAIL_ADMIN, 'downloads', 'file sent');
exit();
}
}
# display file list
foreach ($_SESSION['files'] as $k=>$f) {
echo '<a href="dl.php?id=', $k, '">', $f, "</a><br>\n";
}
?>
--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
[Back to original message]
|