|
Posted by matt on 11/16/06 03:11
Pedro Graca wrote:
> matt wrote:
>
>>Warning: Invalid argument supplied for foreach() in
>>/public_html/file_download.php on line 27
>
>
> When the
>
> $_SESSION['files'] = file_list(DOWNLOAD_DIRECTORY, FILE_VALIDITY_RX);
>
> fails, for whatever reason, $_SESSION['files'] would be false.
> When $_SESSION['files'] is false, the foreach() produces that warning.
>
> In the new 'controlling' script I added a check for $_SESSION['files']
> and introduced the regular expression to check validity of files as
> another define'd constant.
>
> Hope it works now.
>
>
>
>
>
>
>
> <?php
> require 'file_download.inc.php';
>
> ### some constants
> define('DOWNLOAD_DIRECTORY', '.');
> define('EMAIL_ADMIN', 'admin@example.com');
> ### The regular expression will match all files with png, gif, jpg or
> ### bmp extension, in a case insensitive manner
> define('FILE_VALIDITY_RX', '/\.(?:png|gif|jpg|bmp)$/i');
>
> # 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, FILE_VALIDITY_RX);
> }
>
> # 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 links
> if ($_SESSION['files']) {
> foreach ($_SESSION['files'] as $k=>$f) {
> echo '<a href="' . $_SERVER['PHP_SELF'] . '?id=', $k, '">', $f, "</a><br>\n";
> }
> } else {
> echo "No files available to download.<br>\n";
> }
> ?>
>
Thanks again, I really appreciate the work you put in, it's going now,
working perfectly!
[Back to original message]
|