467,161 Members | 982 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

Upload problem...

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="SourceFile_1"/>
I just cannot figure out how to fix this :(
Could somebody please help me?

Thanks in advanced,

-Event

Upload function: (note: TEXT_PREFIX = SourceFile_ UPLOAD_PREFIX =
upload_
################################################## ################

function actionUploadPhotos($goto, $parameters, $message=false) {
global $messageStack, $db;
if (isset($_POST['products_id']) && is_numeric($_POST['products_id'])) {
$the_list = '';
$adjust_max= 'false';
if (isset($_POST['id'])) {
foreach ($_POST['id'] as $key =$value) {
$check = zen_get_attributes_valid($_POST['products_id'], $key,
$value);
if ($check == false) {
$the_list .= TEXT_ERROR_OPTION_FOR . '<span class="alertBlack">'
.. zen_options_name($key) . '</span>' . TEXT_INVALID_SELECTION . '<span
class="alertBlack">' . (zen_values_name($value) == 'TEXT' ?
TEXT_INVALID_USER_INPUT : zen_values_name($value)) . '</span>' . '<br />';
}
}
}
$add_max = zen_get_products_quantity_order_max($_POST['products_id']);
$cart_qty = $this->in_cart_mixed($_POST['products_id']);
$new_qty = $_POST['cart_quantity'];
//echo 'I SEE actionUploadPhotos: ' . $_POST['products_id'] . '<br>';
$new_qty = $this->adjust_quantity($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_products_quantity_order_max($_POST['products_id']) == 1
and $this->in_cart_mixed($_POST['products_id']) == 1)) {
} else {

if ($the_list != '') {
$messageStack->add('product_info', ERROR_CORRECTIONS_HEADING .
$the_list, 'caution');
} else {
$real_ids = isset($_POST['id']) ? $_POST['id'] : "";
if (isset($_GET['number_of_uploads']) &&
$_GET['number_of_uploads'] 0) {
/**
* Need the upload class for attribute type that allows uploads.
*
*/
include(DIR_WS_CLASSES . 'upload2.php');
for ($i = 1, $n = $_GET['number_of_uploads']; $i <= $n; $i++) {
if
(zen_not_null($_FILES['id']['tmp_name'][TEXT_PREFIX .
$_POST[UPLOAD_PREFIX . $i]]) and
($_FILES['id']['tmp_name'][TEXT_PREFIX . $_POST[UPLOAD_PREFIX .
$i]] != 'none')) {
$products_options_file = new upload('id');
$products_options_file->set_destination(DIR_FS_UPLOADS);
$products_options_file->set_output_messages('session');
if ($products_options_file->parse(TEXT_PREFIX .
$_POST[UPLOAD_PREFIX . $i])) {
$products_image_extension =
substr($products_options_file->filename,
strrpos($products_options_file->filename, '.'));

if ($_SESSION['customer_id']) {
$db->Execute("insert into " . TABLE_FILES_UPLOADED . "
(sesskey, customers_id, files_uploaded_name) values('" . zen_session_id() .
"', '" . $_SESSION['customer_id'] . "', '" .
zen_db_input($products_options_file->filename) . "')");
} else {
$db->Execute("insert into " . TABLE_FILES_UPLOADED . "
(sesskey, files_uploaded_name) values('" . zen_session_id() . "', '" .
zen_db_input($products_options_file->filename) . "')");
}
$insert_id = $db->Insert_ID();
$real_ids[TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] =
$insert_id . ". " . $products_options_file->filename;
$products_options_file->set_filename("$insert_id" .
$products_image_extension);
if (!($products_options_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($_POST['products_id'],
$this->get_quantity(zen_get_uprid($_POST['products_id'],
$real_ids))+($new_qty), $real_ids);
// iii 030813 end of changes.
} // eof: set error message
} // eof: quantity maximum = 1

if ($adjust_max == 'true') {
$messageStack->add_session('shopping_cart', ERROR_MAXIMUM_QTY . '
B: - ' . zen_get_products_name($_POST['products_id']), 'caution');
}
}
if ($the_list == '') {
if (DISPLAY_CART == 'false' && $_GET['main_page'] !=
FILENAME_SHOPPING_CART) {
$messageStack->add_session('header', SUCCESS_ADDED_TO_CART_PRODUCT,
'success');
}
zen_redirect(zen_href_link($goto,
zen_get_all_get_params($parameters)));
} 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_FILES'][$this->file])) {
global $HTTP_POST_FILES;

$file = array('name' =$HTTP_POST_FILES[$this->file]['name'],
'type' =$HTTP_POST_FILES[$this->file]['type'],
'size' =$HTTP_POST_FILES[$this->file]['size'],
'tmp_name' =$HTTP_POST_FILES[$this->file]['tmp_name']);
} else {
$file = array('name' =(isset($GLOBALS[$this->file . '_name']) ?
$GLOBALS[$this->file . '_name'] : ''),
'type' =(isset($GLOBALS[$this->file . '_type']) ?
$GLOBALS[$this->file . '_type'] : ''),
'size' =(isset($GLOBALS[$this->file . '_size']) ?
$GLOBALS[$this->file . '_size'] : ''),
'tmp_name' =(isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] :
''));
}
if (!is_uploaded_file($_FILES['tmp_name'])) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', WARNING_NO_FILE_UPLOADED,
'warning');
} else {
$messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED,
'warning');
}
return false;
}

if ( zen_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') &&
is_uploaded_file($file['tmp_name']) ) {
if (zen_not_null($file['size']) and ($file['size'] >
MAX_FILE_UPLOAD_SIZE)) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', ERROR_FILE_TOO_BIG, 'error');
} else {
$messageStack->add_session('upload', ERROR_FILE_TOO_BIG, 'error');
}
return false;
}

if (sizeof($this->extensions) 0) {
if (!in_array(strtolower(substr($file['name'],
strrpos($file['name'], '.')+1)), $this->extensions)) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', ERROR_FILETYPE_NOT_ALLOWED
.. ' ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
} else {
$messageStack->add_session('upload', ERROR_FILETYPE_NOT_ALLOWED
.. ' - ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
}
return false;
}
}

$this->set_file($file);
$this->set_filename($file['name']);
$this->set_tmp_filename($file['tmp_name']);

return $this->check_destination();
} else {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', WARNING_NO_FILE_UPLOADED,
'warning');
} else {
$messageStack->add_session('upload', 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_orig, $height_orig) = getimagesize($this->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 = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($this->destination . $this->filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);
imagejpeg($image_p, $this->destination . '/th/'. $this->filename,
100);
chmod($this->destination . '/th/'. $this->filename,
$this->permissions);
if ($this->message_location == 'direct') {
$messageStack->add_session('header',
SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
} else {
$messageStack->add_session('upload',
SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
}

return true;
} else {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', ERROR_FILE_NOT_SAVED, 'error');
} else {
$messageStack->add_session('upload', 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($permissions);
}

function set_filename($filename) {
$this->filename = $filename;
}

function set_tmp_filename($filename) {
$this->tmp_filename = $filename;
}

function set_extensions($extensions) {
if (zen_not_null($extensions)) {
if (is_array($extensions)) {
$this->extensions = $extensions;
} else {
$this->extensions = array($extensions);
}
} else {
$this->extensions = array();
}
}

function check_destination() {
global $messageStack;

if (!is_writeable($this->destination)) {
if (is_dir($this->destination)) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header',
sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
} else {
$messageStack->add_session('upload',
sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
}
} else {
if ($this->message_location == 'direct') {
$messageStack->add_session('header',
sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
} else {
$messageStack->add_session('upload',
sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
}
}

return false;
} else {
return true;
}
}

function set_output_messages($location) {
switch ($location) {
case 'session':
$this->message_location = 'session';
break;
case 'direct':
default:
$this->message_location = 'direct';
break;
}
}
}



Jun 23 '07 #1
  • viewed: 1890
Share:
2 Replies
Never mind,

I figured it out. I had to read my PHP-book and rewrite parse key function
:)

-Event
"Event Horizon" <ev***@porvoo24.comkirjoitti
viestissä:Tr**************@read3.inet.fi...
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="SourceFile_1"/>
I just cannot figure out how to fix this :(
Could somebody please help me?

Thanks in advanced,

-Event

Upload function: (note: TEXT_PREFIX = SourceFile_ UPLOAD_PREFIX =
upload_
################################################## ################

function actionUploadPhotos($goto, $parameters, $message=false) {
global $messageStack, $db;
if (isset($_POST['products_id']) && is_numeric($_POST['products_id']))
{
$the_list = '';
$adjust_max= 'false';
if (isset($_POST['id'])) {
foreach ($_POST['id'] as $key =$value) {
$check = zen_get_attributes_valid($_POST['products_id'], $key,
$value);
if ($check == false) {
$the_list .= TEXT_ERROR_OPTION_FOR . '<span
class="alertBlack">'
. zen_options_name($key) . '</span>' . TEXT_INVALID_SELECTION . '<span
class="alertBlack">' . (zen_values_name($value) == 'TEXT' ?
TEXT_INVALID_USER_INPUT : zen_values_name($value)) . '</span>' . '<br />';
}
}
}
$add_max =
zen_get_products_quantity_order_max($_POST['products_id']);
$cart_qty = $this->in_cart_mixed($_POST['products_id']);
$new_qty = $_POST['cart_quantity'];
//echo 'I SEE actionUploadPhotos: ' . $_POST['products_id'] . '<br>';
$new_qty = $this->adjust_quantity($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_products_quantity_order_max($_POST['products_id']) == 1
and $this->in_cart_mixed($_POST['products_id']) == 1)) {
} else {

if ($the_list != '') {
$messageStack->add('product_info', ERROR_CORRECTIONS_HEADING .
$the_list, 'caution');
} else {
$real_ids = isset($_POST['id']) ? $_POST['id'] : "";
if (isset($_GET['number_of_uploads']) &&
$_GET['number_of_uploads'] 0) {
/**
* Need the upload class for attribute type that allows
uploads.
*
*/
include(DIR_WS_CLASSES . 'upload2.php');
for ($i = 1, $n = $_GET['number_of_uploads']; $i <= $n; $i++) {
if
(zen_not_null($_FILES['id']['tmp_name'][TEXT_PREFIX .
$_POST[UPLOAD_PREFIX . $i]]) and
($_FILES['id']['tmp_name'][TEXT_PREFIX . $_POST[UPLOAD_PREFIX .
$i]] != 'none')) {
$products_options_file = new upload('id');
$products_options_file->set_destination(DIR_FS_UPLOADS);
$products_options_file->set_output_messages('session');
if ($products_options_file->parse(TEXT_PREFIX .
$_POST[UPLOAD_PREFIX . $i])) {
$products_image_extension =
substr($products_options_file->filename,
strrpos($products_options_file->filename, '.'));

if ($_SESSION['customer_id']) {
$db->Execute("insert into " . TABLE_FILES_UPLOADED . "
(sesskey, customers_id, files_uploaded_name) values('" . zen_session_id()
.
"', '" . $_SESSION['customer_id'] . "', '" .
zen_db_input($products_options_file->filename) . "')");
} else {
$db->Execute("insert into " . TABLE_FILES_UPLOADED . "
(sesskey, files_uploaded_name) values('" . zen_session_id() . "', '" .
zen_db_input($products_options_file->filename) . "')");
}
$insert_id = $db->Insert_ID();
$real_ids[TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] =
$insert_id . ". " . $products_options_file->filename;
$products_options_file->set_filename("$insert_id" .
$products_image_extension);
if (!($products_options_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($_POST['products_id'],
$this->get_quantity(zen_get_uprid($_POST['products_id'],
$real_ids))+($new_qty), $real_ids);
// iii 030813 end of changes.
} // eof: set error message
} // eof: quantity maximum = 1

if ($adjust_max == 'true') {
$messageStack->add_session('shopping_cart', ERROR_MAXIMUM_QTY . '
B: - ' . zen_get_products_name($_POST['products_id']), 'caution');
}
}
if ($the_list == '') {
if (DISPLAY_CART == 'false' && $_GET['main_page'] !=
FILENAME_SHOPPING_CART) {
$messageStack->add_session('header', SUCCESS_ADDED_TO_CART_PRODUCT,
'success');
}
zen_redirect(zen_href_link($goto,
zen_get_all_get_params($parameters)));
} 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_FILES'][$this->file])) {
global $HTTP_POST_FILES;

$file = array('name' =$HTTP_POST_FILES[$this->file]['name'],
'type' =$HTTP_POST_FILES[$this->file]['type'],
'size' =$HTTP_POST_FILES[$this->file]['size'],
'tmp_name' =$HTTP_POST_FILES[$this->file]['tmp_name']);
} else {
$file = array('name' =(isset($GLOBALS[$this->file . '_name']) ?
$GLOBALS[$this->file . '_name'] : ''),
'type' =(isset($GLOBALS[$this->file . '_type']) ?
$GLOBALS[$this->file . '_type'] : ''),
'size' =(isset($GLOBALS[$this->file . '_size']) ?
$GLOBALS[$this->file . '_size'] : ''),
'tmp_name' =(isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] :
''));
}
if (!is_uploaded_file($_FILES['tmp_name'])) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', WARNING_NO_FILE_UPLOADED,
'warning');
} else {
$messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED,
'warning');
}
return false;
}

if ( zen_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none')
&&
is_uploaded_file($file['tmp_name']) ) {
if (zen_not_null($file['size']) and ($file['size'] >
MAX_FILE_UPLOAD_SIZE)) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', ERROR_FILE_TOO_BIG,
'error');
} else {
$messageStack->add_session('upload', ERROR_FILE_TOO_BIG,
'error');
}
return false;
}

if (sizeof($this->extensions) 0) {
if (!in_array(strtolower(substr($file['name'],
strrpos($file['name'], '.')+1)), $this->extensions)) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', ERROR_FILETYPE_NOT_ALLOWED
. ' ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
} else {
$messageStack->add_session('upload', ERROR_FILETYPE_NOT_ALLOWED
. ' - ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
}
return false;
}
}

$this->set_file($file);
$this->set_filename($file['name']);
$this->set_tmp_filename($file['tmp_name']);

return $this->check_destination();
} else {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', WARNING_NO_FILE_UPLOADED,
'warning');
} else {
$messageStack->add_session('upload', 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_orig, $height_orig) = getimagesize($this->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 = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($this->destination .
$this->filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);
imagejpeg($image_p, $this->destination . '/th/'. $this->filename,
100);
chmod($this->destination . '/th/'. $this->filename,
$this->permissions);
if ($this->message_location == 'direct') {
$messageStack->add_session('header',
SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
} else {
$messageStack->add_session('upload',
SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
}

return true;
} else {
if ($this->message_location == 'direct') {
$messageStack->add_session('header', ERROR_FILE_NOT_SAVED,
'error');
} else {
$messageStack->add_session('upload', 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($permissions);
}

function set_filename($filename) {
$this->filename = $filename;
}

function set_tmp_filename($filename) {
$this->tmp_filename = $filename;
}

function set_extensions($extensions) {
if (zen_not_null($extensions)) {
if (is_array($extensions)) {
$this->extensions = $extensions;
} else {
$this->extensions = array($extensions);
}
} else {
$this->extensions = array();
}
}

function check_destination() {
global $messageStack;

if (!is_writeable($this->destination)) {
if (is_dir($this->destination)) {
if ($this->message_location == 'direct') {
$messageStack->add_session('header',
sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
} else {
$messageStack->add_session('upload',
sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
}
} else {
if ($this->message_location == 'direct') {
$messageStack->add_session('header',
sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
} else {
$messageStack->add_session('upload',
sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
}
}

return false;
} else {
return true;
}
}

function set_output_messages($location) {
switch ($location) {
case 'session':
$this->message_location = 'session';
break;
case 'direct':
default:
$this->message_location = 'direct';
break;
}
}
}



Jun 24 '07 #2
Event Horizon wrote:
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="SourceFile_1"/>
I just cannot figure out how to fix this :(
Could somebody please help me?
Either fix the script or work around it :-)

something like
if($_FILES['SourceFile_1'])
{
$_FILES[id['SourceFile_1']]=$_FILES['SourceFile_1'];
}
Not the nicest solution however :-)

Arjen
Jun 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by dave | last post: by
4 posts views Thread by NohaKhalifa | last post: by
2 posts views Thread by Gary \(Girish\) | last post: by
9 posts views Thread by Wayne Smith | last post: by
6 posts views Thread by Vic Spainhower | last post: by
7 posts views Thread by pbd22 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.