Re: HELP! Trying to add Email Notification to Download File Code



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*.
.



Relevant Pages

  • Re: =sessions= [J. Meloni Textbook]
    ... The foreach function is for arrays only. ... Is $_SESSIONan array or just an element in an array? ... > Warning: Invalid argument supplied for foreachin ...
    (alt.php)
  • Re: fgets() - supposed to be simple :)
    ... shouldn't) use the macro with a semicolon. ... And you need an array of 100 floats for the amount of the purchase? ... >void menu; ... This is an error, not a warning. ...
    (comp.lang.c)
  • Re: Q: Checking the size of a non-allocated array?
    ... an actual argument is already invalid ... First note that you don't have an unallocated array in the subroutine. ... it is comparable to disassociated or undefined pointers. ... Obviously the compiler has ...
    (comp.lang.fortran)
  • Re: Macys Parade - Unicycling Video Clip
    ... I think the whole gallery is messed up.. ... Warning: Invalid argument supplied for foreach() in ...
    (rec.sport.unicycling)
  • Re: Critique my assignment please
    ... foo.c:44: warning: no previous prototype for `NumberFromUpperLetter' ... Debug Mode: SAFE because assertion fails if input is invalid ... unsigned NumberFromUpperLetter(char const x) ...
    (comp.lang.c)

Loading