Hi,
I'm trying to add an simple upload applet to shopping cart script. My new
applet form sends all needed post fields ( quantity, product, etc... )
but the "file" post field is hardcoded in applet.
Shop script works with :
<input type="file" name="id[SourceFile_1]"/>
but not with applet's:
<input type="file" name="SourceFil e_1"/>
I just cannot figure out how to fix this :(
Could somebody please help me?
Thanks in advanced,
-Event
Upload function:
############### ############### ############### ############### ######
function actionUploadPho tos($goto, $parameters, $message=false) {
global $messageStack, $db;
if (isset($_POST['products_id']) && is_numeric($_PO ST['products_id'])) {
$the_list = '';
$adjust_max= 'false';
if (isset($_POST['id'])) {
foreach ($_POST['id'] as $key =$value) {
$check = zen_get_attribu tes_valid($_POS T['products_id'], $key,
$value);
if ($check == false) {
$the_list .= TEXT_ERROR_OPTI ON_FOR . '<span class="alertBla ck">'
.. zen_options_nam e($key) . '</span>' . TEXT_INVALID_SE LECTION . '<span
class="alertBla ck">' . (zen_values_nam e($value) == 'TEXT' ?
TEXT_INVALID_US ER_INPUT : zen_values_name ($value)) . '</span>' . '<br />';
}
}
}
$add_max = zen_get_product s_quantity_orde r_max($_POST['products_id']);
$cart_qty = $this->in_cart_mixed( $_POST['products_id']);
$new_qty = $_POST['cart_quantity'];
//echo 'I SEE actionUploadPho tos: ' . $_POST['products_id'] . '<br>';
$new_qty = $this->adjust_quantit y($new_qty, $_POST['products_id'],
'shopping_cart' );
if (($add_max == 1 and $cart_qty == 1)) {
// do not add
$new_qty = 0;
$adjust_max= 'true';
} else {
// adjust quantity if needed
if (($new_qty + $cart_qty $add_max) and $add_max != 0) {
$adjust_max= 'true';
$new_qty = $add_max - $cart_qty;
}
}
if ((zen_get_produ cts_quantity_or der_max($_POST['products_id']) == 1
and $this->in_cart_mixed( $_POST['products_id']) == 1)) {
} else {
if ($the_list != '') {
$messageStack->add('product_i nfo', ERROR_CORRECTIO NS_HEADING .
$the_list, 'caution');
} else {
$real_ids = isset($_POST['id']) ? $_POST['id'] : "";
if (isset($_GET['number_of_uplo ads']) &&
$_GET['number_of_uplo ads'] 0) {
/**
* Need the upload class for attribute type that allows uploads.
*
*/
include(DIR_WS_ CLASSES . 'upload2.php');
for ($i = 1, $n = $_GET['number_of_uplo ads']; $i <= $n; $i++) {
if
(zen_not_null($ _FILES['SourceFile_1']['tmp_name'][TEXT_PREFIX .
$_POST[UPLOAD_PREFIX . $i]]) and
($_FILES['SourceFile_1']['tmp_name'][TEXT_PREFIX . $_POST[UPLOAD_PREFIX .
$i]] != 'none')) {
$products_optio ns_file = new upload('id');
$products_optio ns_file->set_destinatio n(DIR_FS_UPLOAD S);
$products_optio ns_file->set_output_mes sages('session' );
if ($products_opti ons_file->parse(TEXT_PRE FIX .
$_POST[UPLOAD_PREFIX . $i])) {
$products_image _extension =
substr($product s_options_file->filename,
strrpos($produc ts_options_file->filename, '.'));
if ($_SESSION['customer_id']) {
$db->Execute("inser t into " . TABLE_FILES_UPL OADED . "
(sesskey, customers_id, files_uploaded_ name) values('" . zen_session_id( ) .
"', '" . $_SESSION['customer_id'] . "', '" .
zen_db_input($p roducts_options _file->filename) . "')");
} else {
$db->Execute("inser t into " . TABLE_FILES_UPL OADED . "
(sesskey, files_uploaded_ name) values('" . zen_session_id( ) . "', '" .
zen_db_input($p roducts_options _file->filename) . "')");
}
$insert_id = $db->Insert_ID();
$real_ids[TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] =
$insert_id . ". " . $products_optio ns_file->filename;
$products_optio ns_file->set_filename(" $insert_id" .
$products_image _extension);
if (!($products_op tions_file->save())) {
break;
}
} else {
break;
}
} else { // No file uploaded -- use previous value
$real_ids[TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] =
$_POST[TEXT_PREFIX . UPLOAD_PREFIX . $i];
}
}
}
$this->add_cart($_POS T['products_id'],
$this->get_quantity(z en_get_uprid($_ POST['products_id'],
$real_ids))+($n ew_qty), $real_ids);
// iii 030813 end of changes.
} // eof: set error message
} // eof: quantity maximum = 1
if ($adjust_max == 'true') {
$messageStack->add_session('s hopping_cart', ERROR_MAXIMUM_Q TY . '
B: - ' . zen_get_product s_name($_POST['products_id']), 'caution');
}
}
if ($the_list == '') {
if (DISPLAY_CART == 'false' && $_GET['main_page'] !=
FILENAME_SHOPPI NG_CART) {
$messageStack->add_session('h eader', SUCCESS_ADDED_T O_CART_PRODUCT,
'success');
}
zen_redirect(ze n_href_link($go to,
zen_get_all_get _params($parame ters)));
} else {
}
}
############### ############### ############### ############### ############### ############### ##########
in upload2.php ( parse function and save )
function parse($key = '') {
global $messageStack;
if (isset($_FILES[$this->file])) {
if (zen_not_null($ key)) {
$file = array('name' =$_FILES[$this->file]['name'][$key],
'type' =$_FILES[$this->file]['type'][$key],
'size' =$_FILES[$this->file]['size'][$key],
'tmp_name' =$_FILES['file']['tmp_name']);
} else {
$file = array('name' =$_FILES[$this->file]['name'],
'type' =$_FILES[$this->file]['type'],
'size' =$_FILES[$this->file]['size'],
'tmp_name' =$_FILES['file']['tmp_name']);
}
} elseif (isset($GLOBALS['HTTP_POST_FILE S'][$this->file])) {
global $HTTP_POST_FILE S;
$file = array('name' =$HTTP_POST_FIL ES[$this->file]['name'],
'type' =$HTTP_POST_FIL ES[$this->file]['type'],
'size' =$HTTP_POST_FIL ES[$this->file]['size'],
'tmp_name' =$HTTP_POST_FIL ES[$this->file]['tmp_name']);
} else {
$file = array('name' =(isset($GLOBAL S[$this->file . '_name']) ?
$GLOBALS[$this->file . '_name'] : ''),
'type' =(isset($GLOBAL S[$this->file . '_type']) ?
$GLOBALS[$this->file . '_type'] : ''),
'size' =(isset($GLOBAL S[$this->file . '_size']) ?
$GLOBALS[$this->file . '_size'] : ''),
'tmp_name' =(isset($GLOBAL S[$this->file]) ? $GLOBALS[$this->file] :
''));
}
if (!is_uploaded_f ile($_FILES['tmp_name'])) {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader', WARNING_NO_FILE _UPLOADED,
'warning');
} else {
$messageStack->add_session('u pload', WARNING_NO_FILE _UPLOADED,
'warning');
}
return false;
}
if ( zen_not_null($f ile['tmp_name']) && ($file['tmp_name'] != 'none') &&
is_uploaded_fil e($file['tmp_name']) ) {
if (zen_not_null($ file['size']) and ($file['size'] >
MAX_FILE_UPLOAD _SIZE)) {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader', ERROR_FILE_TOO_ BIG, 'error');
} else {
$messageStack->add_session('u pload', ERROR_FILE_TOO_ BIG, 'error');
}
return false;
}
if (sizeof($this->extensions) 0) {
if (!in_array(strt olower(substr($ file['name'],
strrpos($file['name'], '.')+1)), $this->extensions)) {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader', ERROR_FILETYPE_ NOT_ALLOWED
.. ' ' . UPLOAD_FILENAME _EXTENSIONS, 'error');
} else {
$messageStack->add_session('u pload', ERROR_FILETYPE_ NOT_ALLOWED
.. ' - ' . UPLOAD_FILENAME _EXTENSIONS, 'error');
}
return false;
}
}
$this->set_file($file );
$this->set_filename($ file['name']);
$this->set_tmp_filena me($file['tmp_name']);
return $this->check_destinat ion();
} else {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader', WARNING_NO_FILE _UPLOADED,
'warning');
} else {
$messageStack->add_session('u pload', WARNING_NO_FILE _UPLOADED,
'warning');
}
return false;
}
}
function save() {
global $messageStack;
if (substr($this->destination, -1) != '/') $this->destination .= '/';
if (move_uploaded_ file($this->file['tmp_name'], $this->destination .
$this->filename)) {
$width = 100;
$height = 100;
list($width_ori g, $height_orig) = getimagesize($t his->destination .
$this->filename);
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetrue color($width, $height);
$image = imagecreatefrom jpeg($this->destination . $this->filename);
imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);
imagejpeg($imag e_p, $this->destination . '/th/'. $this->filename,
100);
chmod($this->destination . '/th/'. $this->filename,
$this->permissions) ;
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader',
SUCCESS_FILE_SA VED_SUCCESSFULL Y, 'success');
} else {
$messageStack->add_session('u pload',
SUCCESS_FILE_SA VED_SUCCESSFULL Y, 'success');
}
return true;
} else {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader', ERROR_FILE_NOT_ SAVED, 'error');
} else {
$messageStack->add_session('u pload', ERROR_FILE_NOT_ SAVED, 'error');
}
return false;
}
}
function set_file($file) {
$this->file = $file;
}
function set_destination ($destination) {
$this->destination = $destination;
}
function set_permissions ($permissions) {
$this->permissions = octdec($permiss ions);
}
function set_filename($f ilename) {
$this->filename = $filename;
}
function set_tmp_filenam e($filename) {
$this->tmp_filename = $filename;
}
function set_extensions( $extensions) {
if (zen_not_null($ extensions)) {
if (is_array($exte nsions)) {
$this->extensions = $extensions;
} else {
$this->extensions = array($extensio ns);
}
} else {
$this->extensions = array();
}
}
function check_destinati on() {
global $messageStack;
if (!is_writeable( $this->destination) ) {
if (is_dir($this->destination) ) {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader',
sprintf(ERROR_D ESTINATION_NOT_ WRITEABLE, $this->destination) , 'error');
} else {
$messageStack->add_session('u pload',
sprintf(ERROR_D ESTINATION_NOT_ WRITEABLE, $this->destination) , 'error');
}
} else {
if ($this->message_locati on == 'direct') {
$messageStack->add_session('h eader',
sprintf(ERROR_D ESTINATION_DOES _NOT_EXIST, $this->destination) , 'error');
} else {
$messageStack->add_session('u pload',
sprintf(ERROR_D ESTINATION_DOES _NOT_EXIST, $this->destination) , 'error');
}
}
return false;
} else {
return true;
}
}
function set_output_mess ages($location) {
switch ($location) {
case 'session':
$this->message_locati on = 'session';
break;
case 'direct':
default:
$this->message_locati on = 'direct';
break;
}
}
}