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

Your name:

Reply:


Posted by Pedro Graca on 11/16/06 00:17

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";
}
?>

--
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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация