|
Posted by Pedro Graca on 11/16/06 00:17
matt wrote:
> Pedro Graca wrote:
>>
>> This warning will go away once you get the headers sorted out.
>>
> Ok fixed that, not just this error:
>
> Warning: Invalid argument supplied for foreach() in
> /public_html/file_download.php on line 27
Oops ... /That/ wasn't supposed to happen :)
New version of file_download.inc.php (no spaces in front); there's also
a new version of the 'controlling' script. I'll post it in another
article with my reasoning for why the first version failed.
<?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: 20061115
Author: Pedro Graca
History:
version 20061115
added regexp as a parameter to
download_file_is_valid() and file_list()
removed unnecessary clearstatcache() from
download_file_is_valid()
version 20061114
first draft
****************************************************** */
######## 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, $rx)
# verifies if the $name is valid for download.
# validity is checked with the regualr expression $rx
#
# Returns true or false
#
function download_file_is_valid($name, $rx) {
if (!is_file($name)) return false;
return preg_match($rx, $name);
}
######## file_list($d, $rx)
# builds an array with all valid files in the
# $d directory.
# File validity is tested with the regular expression $rx.
#
# 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, $rx) {
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, $rx)) {
$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;
}
?>
--
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*.
Navigation:
[Reply to this message]
|