Jump to content

bsoder

Members
  • Content count

    5
  • Joined

  • Last visited

Profile Information

  • Real Name
    Brian Soderblom
  1. I already had Option Type Feature 1.7.2 installed, and needed file upload capability, but this module appears to be fairly old and not built on the current release of OSCommerce (?) - in any case, I used it as a guide and modified my setup to get file uploading working. I have a bunch of extra modules installed so not 100% sure this will work for a vanilla install with OTF 1.7.2, but I didn't see anything that appeared to be related to one of my other changes in the relevant sections. This feature should probably be rolled into OTF anyway. I hope this is useful to someone. Here's my code. ------------------------------- Modified files: product_info.php admin/products_attributes.php includes/application_top.php includes/configure.php includes/database_tables.php includes/functions/html_output.php includes/languages/english.php New file: includes/classes/upload.php ################################################################### SQL to create files_uploaded table =================================================================== CREATE TABLE `files_uploaded` ( `files_uploaded_id` INT( 11 ) NOT NULL AUTO_INCREMENT , `sesskey` VARCHAR( 32 ) , `customers_id` INT( 11 ) , `files_uploaded_name` VARCHAR( 64 ) NOT NULL , PRIMARY KEY ( `files_uploaded_id` ) ) COMMENT = 'Must always have either a sesskey or customers_id'; ################################################################### ################################################################### product_info.php =================================================================== ### FIND <p><?php echo stripslashes($product_info['products_description']); ?></p> <?php ### ADD AFTER $number_of_uploads = 0; =================================================================== ### FIND case PRODUCTS_OPTIONS_TYPE_TEXTAREA: ### ADD BEFORE case PRODUCTS_OPTIONS_TYPE_FILE: //CLR 030714 Add logic for text option $number_of_uploads++; $products_attribs_query = tep_db_query("select distinct patrib.options_values_price, patrib.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = '" . $products_options_name['products_options_id'] . "'"); $products_attribs_array = tep_db_fetch_array($products_attribs_query); $tmp_html = '<input type="file" name ="id[' . TEXT_PREFIX . $products_options_name['products_options_id'] . ']" size="' . $products_options_name['products_options_length'] .'">'; if ($products_attribs_array['options_values_price'] != '0') { $tmp_html .= '(' . $products_attribs_array['price_prefix'] . $currencies->display_price($products_attribs_array['options_values_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) .')'; } ?> <tr> <td class="main"><?php echo $products_options_name['products_options_name'] . ':'; ?></td> <!-- <td class="main"><?php echo $tmp_html; ?></td> --> <td class="main"><input type="file" name="id[<?php echo TEXT_PREFIX . $products_options_name['products_options_id']; ?>]"><br> <?php echo $cart->contents[$HTTP_GET_VARS['products_id']]['attributes_values'][$products_options_name['products_options_id']] . tep_draw_hidden_field(UPLOAD_PREFIX . $number_of_uploads, $products_options_name['products_options_id']) . tep_draw_hidden_field(TEXT_PREFIX . UPLOAD_PREFIX . $number_of_uploads, $cart->contents[$HTTP_GET_VARS['products_id']]['attributes_values'][$products_options_name['products_options_id']]); ?></td> </tr> <?php break; =================================================================== ### FIND </table></form></td> ### ADD BEFORE <tr> <td><?php echo tep_draw_hidden_field('number_of_uploads', $number_of_uploads); ?></td> </tr> ################################################################### ################################################################### admin/products_attributes.php =================================================================== ### FIND $values[] = array('id' => 4, 'text' => 'Textarea'); ### ADD AFTER $values[] = array('id' => 5, 'text' => 'File'); =================================================================== ### FIND if ($opt_type == 4) return 'Textarea'; ### ADD AFTER if ($opt_type == 5) return 'File'; ################################################################### ################################################################### includes/application_top.php =================================================================== ### FIND $navigation->add_current_page(); ### ADD AFTER // infobox require(DIR_WS_CLASSES . 'boxes.php'); // initialize the message stack for output messages require(DIR_WS_CLASSES . 'message_stack.php'); $messageStack = new messageStack; =================================================================== ### FIND // customer adds a product from the products page case 'add_product' : if (isset($HTTP_POST_VARS['products_id']) && is_numeric($HTTP_POST_VARS['products_id'])) { $cart->add_cart($HTTP_POST_VARS['products_id'], $cart->get_quantity(tep_get_uprid($HTTP_POST_VARS['products_id'], $HTTP_POST_VARS['id']))+1, $HTTP_POST_VARS['id']); } tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters))); break; ### REPLACE WITH // customer adds a product from the products page case 'add_product' : if (isset($HTTP_POST_VARS['products_id']) && is_numeric($HTTP_POST_VARS['products_id'])) { $real_ids = $HTTP_POST_VARS['id']; if ($HTTP_POST_VARS['number_of_uploads'] > 0) { require(DIR_WS_CLASSES . 'upload.php'); for ($i = 1; $i <= $HTTP_POST_VARS['number_of_uploads']; $i++) { if (tep_not_null($_FILES['id']['tmp_name'][TEXT_PREFIX . $HTTP_POST_VARS[UPLOAD_PREFIX . $i]]) and ($_FILES['id']['tmp_name'][TEXT_PREFIX . $HTTP_POST_VARS[UPLOAD_PREFIX . $i]] != 'none')) { $products_options_file = new upload('id'); $products_options_file->set_destination(DIR_FS_UPLOADS); if ($products_options_file->parse(TEXT_PREFIX . $HTTP_POST_VARS[UPLOAD_PREFIX . $i])) { if (tep_session_is_registered('customer_id')) { tep_db_query("insert into " . TABLE_FILES_UPLOADED . " (sesskey, customers_id, files_uploaded_name) values('" . tep_session_id() . "', '" . $customer_id . "', '" . tep_db_input($products_options_file->filename) . "')"); } else { tep_db_query("insert into " . TABLE_FILES_UPLOADED . " (sesskey, files_uploaded_name) values('" . tep_session_id() . "', '" . tep_db_input($products_options_file->filename) . "')"); } $insert_id = tep_db_insert_id(); $real_ids[TEXT_PREFIX . $HTTP_POST_VARS[UPLOAD_PREFIX . $i]] = $insert_id . "_" . $products_options_file->filename; $products_options_file->set_filename("$insert_id"); if (!($products_options_file->save())) { break 2; } } else { break 2; } } else { // No file uploaded -- use previous value $real_ids[TEXT_PREFIX . $HTTP_POST_VARS[UPLOAD_PREFIX . $i]] = $HTTP_POST_VARS[TEXT_PREFIX . UPLOAD_PREFIX . $i]; } } } $cart->add_cart($HTTP_POST_VARS['products_id'], $cart->get_quantity(tep_get_uprid($HTTP_POST_VARS['products_id'], $real_ids))+1, $real_ids); } tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters))); break; =================================================================== ### FIND (towards the bottom of the file, not the one entered above) require(DIR_WS_CLASSES . 'boxes.php'); ### REPLACE WITH // require(DIR_WS_CLASSES . 'boxes.php'); =================================================================== ### FIND (towards the bottom of the file, not the one entered above) require(DIR_WS_CLASSES . 'message_stack.php'); $messageStack = new messageStack; ### REPLACE WITH // require(DIR_WS_CLASSES . 'message_stack.php'); // $messageStack = new messageStack; ################################################################### ################################################################### includes/configure.php =================================================================== ### FIND define('PRODUCTS_OPTIONS_TYPE_TEXTAREA', 4); ### ADD AFTER define('PRODUCTS_OPTIONS_TYPE_FILE', 5); define('UPLOAD_PREFIX', 'upl_'); define('DIR_FS_UPLOADS', 'REPLACE WITH FULL PATH TO UPLOAD DIRECTORY'); ################################################################### ################################################################### includes/database_tables.php =================================================================== ### ADD BEFORE CLOSING ?> define('TABLE_FILES_UPLOADED', 'files_uploaded'); ################################################################### ################################################################### includes/functions/html_output.php =================================================================== ### FIND $form = '<form name="' . tep_output_string($name) . '" action="' . tep_output_string($action) . '" method="' . tep_output_string($method) . '"'; ### REPLACE WITH $form = '<form name="' . tep_output_string($name) . '" action="' . tep_output_string($action) . '" method="' . tep_output_string($method) . '" enctype="multipart/form-data"'; ################################################################### ################################################################### includes/languages/english.php =================================================================== ### ADD BEFORE CLOSING ?> define('ERROR_FILETYPE_NOT_ALLOWED', 'Error: File type not allowed.'); define('WARNING_NO_FILE_UPLOADED', 'Warning: no file uploaded.'); define('SUCCESS_FILE_SAVED_SUCCESSFULLY', 'Success: file saved successfully.'); define('ERROR_FILE_NOT_SAVED', 'Error: file not saved.'); define('ERROR_DESTINATION_NOT_WRITEABLE', 'Error: destination not writeable.'); define('ERROR_DESTINATION_DOES_NOT_EXIST', 'Error: destination does not exist.'); ################################################################### ################################################################### NEW FILE: includes/classes/upload.php =================================================================== <?php /* $Id: upload.php,v 1.2 2003/06/20 00:18:30 hpdl Exp $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2003 osCommerce Released under the GNU General Public License */ class upload { var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location; function upload($file = '', $destination = '', $permissions = '777', $extensions = '') { $this->set_file($file); $this->set_destination($destination); $this->set_permissions($permissions); $this->set_extensions($extensions); $this->set_output_messages('direct'); if (tep_not_null($this->file) && tep_not_null($this->destination)) { $this->set_output_messages('session'); if ( ($this->parse() == true) && ($this->save() == true) ) { return true; } else { return false; } } } function parse($key = '') { global $messageStack; $file = array('name' => $_FILES[$this->file]['name'][$key], 'type' => $_FILES[$this->file]['type'][$key], 'size' => $_FILES[$this->file]['size'][$key], 'tmp_name' => $_FILES[$this->file]['tmp_name'][$key]); if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') ) { 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(ERROR_FILETYPE_NOT_ALLOWED, 'error'); } else { $messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, '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(WARNING_NO_FILE_UPLOADED, 'warning'); } else { $messageStack->add_session(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 . "_" . $this->file['name'])) { chmod($this->destination . $this->filename, $this->permissions); if ($this->message_location == 'direct') { $messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success'); } else { $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success'); } return true; } else { if ($this->message_location == 'direct') { $messageStack->add(ERROR_FILE_NOT_SAVED, 'error'); } else { $messageStack->add_session(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 (tep_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(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); } else { $messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error'); } } else { if ($this->message_location == 'direct') { $messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error'); } else { $messageStack->add_session(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; } } } ?>
  2. I already have Option Type Feature 1.7.2 installed. I'd like to add the ability to upload a picture as well - can I do that using this module, or is there a better alternative given what I already have set up?
×