Jump to content
Latest News: (loading..)
Sign in to follow this  
Druide

[Contribution] Setting upload image destination...

Recommended Posts

Original thread was started at Tips & Tricks Forum,

but i decided to throw this into the contribution area for further development (?)

 

Setting upload image destination in categories.php, Admin change

 

This code allows one to do three things:

 

1. Change the filename of an image at time of upload.

 

2. Put the image in a subdirectory of images/

 

3. Change the product or category image location in the database without uploading a new image. If the image name is blank and none is uploaded, then it will erase the image.

 

In admin/categories.php, around lines 80-2, change from

        if ($categories_image = new upload('categories_image', DIR_FS_CATALOG_IMAGES)) {
         tep_db_query("update " . TABLE_CATEGORIES . " set categories_image = '" . tep_db_input($categories_image->filename) . "' where categories_id = '" . (int)$categories_id . "'");
       }

to

      $categories_image = new upload('categories_image');
     $categories_image_name = '';
     if (isset($HTTP_POST_VARS['image_destination']) && tep_not_null($HTTP_POST_VARS['image_destination'])) {
       $categories_image->set_destination(dirname(DIR_FS_CATALOG_IMAGES . $HTTP_POST_VARS['image_destination']));
       $categories_image_name = $HTTP_POST_VARS['image_destination'];
     } else {
       $categories_image->set_destination(DIR_FS_CATALOG_IMAGES);
     }
     if ($categories_image->parse()) {
       if (isset($HTTP_POST_VARS['image_destination']) && tep_not_null($HTTP_POST_VARS['image_destination'])) {
         $categories_image->set_filename(basename(DIR_FS_CATALOG_IMAGES . $HTTP_POST_VARS['image_destination']));
       }
       if ($categories_image->save()) {
         $categories_image_name = $categories_image->filename;
       }
     }
     if (isset($HTTP_POST_VARS['image_destination']) && tep_not_null($HTTP_POST_VARS['image_destination'])) {
       $categories_image_name = $HTTP_POST_VARS['image_destination'];
     }
     tep_db_query("update " . TABLE_CATEGORIES . " set categories_image = '" . tep_db_input($categories_image_name) . "' where categories_id = '" . (int)$categories_id . "'");

 

Around lines 224-6, change from

          if (isset($HTTP_POST_VARS['products_image']) && tep_not_null($HTTP_POST_VARS['products_image']) && ($HTTP_POST_VARS['products_image'] != 'none')) {
           $sql_data_array['products_image'] = tep_db_prepare_input($HTTP_POST_VARS['products_image']);
         }

to

          $sql_data_array['products_image'] = tep_db_prepare_input($HTTP_POST_VARS['products_image']);

 

Around lines 315-20, change from

        $products_image->set_destination(DIR_FS_CATALOG_IMAGES);
       if ($products_image->parse() && $products_image->save()) {
         $products_image_name = $products_image->filename;
       } else {
         $products_image_name = (isset($HTTP_POST_VARS['products_previous_image']) ? $HTTP_POST_VARS['products_previous_image'] : '');
       }

to

(this BLOCK has been changed by Robert Hellemans)

        if (isset($HTTP_POST_VARS['image_destination'])) {
 // BOF ftpmkdir by robert hellemans
	 // split the 'new' directory and file from eachother
	 $image_destination_dir = explode("/" , $HTTP_POST_VARS['image_destination']);
  	 // check if the 'new' catalog image directory exists
  	 if (is_dir(DIR_FS_CATALOG_IMAGES . $image_destination_dir[0])) {
    	 // uncomment the next line if you want to debug
    	 // $messageStack->add(DIR_WS_IMAGES . $image_destination_dir[0] . " already exists", 'caution');
  	 } else {
     include(DIR_WS_INCLUDES . 'ftpmkdir.php');
   FtpMkdir(DIR_FTP_MKDIR, $image_destination_dir[0]);
    	 if (!is_writeable(DIR_FS_CATALOG_IMAGES . $image_destination_dir[0])) $messageStack->add(DIR_WS_IMAGES . $image_destination_all[0] . " is NOT writable", 'error');
    	 if (is_writeable(DIR_FS_CATALOG_IMAGES . $image_destination_dir[0])) $messageStack->add("Success: Directory " . DIR_WS_IMAGES . $image_destination_all[0] . " is created & CHMOD 777", 'success');
  	 }
   // EOF ftpmkdir
         $products_image->set_destination(dirname(DIR_FS_CATALOG_IMAGES . $HTTP_POST_VARS['image_destination']));
         $products_image->set_filename(basename(DIR_FS_CATALOG_IMAGES . $HTTP_POST_VARS['image_destination']));
       } else {
         $products_image->set_destination(DIR_FS_CATALOG_IMAGES);
       }
       if ($products_image->parse() && $products_image->save()) {
           $products_image_name = $products_image->filename;
       } else {
         $products_image_name = '';
       }
       if (isset($HTTP_POST_VARS['image_destination'])) {
         $products_image_name = $HTTP_POST_VARS['image_destination'];
       }

 

About the above block:

What does it do ? Well this little ADDON creates for you a new directory ON_THE_FLY and then uploads the image into it.

So you Dont need to create a new directory with your FTP client and then change it to CHMOD 777, and you know why ?

This little tool does it ALL for you

 

Cool or not, i think its AWESOME

I had an idea again and started to work on it and VOILA i made it work,

this can be used also for those error 'not writable directories' messages that you get after you do a clean OSC installation (for experts ONLY )

 

 

Around lines 562-5, change from

          <tr>
           <td class="main"><?php echo TEXT_PRODUCTS_IMAGE; ?></td>
           <td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . tep_draw_file_field('products_image') . '<br>' . tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . $pInfo->products_image . tep_draw_hidden_field('products_previous_image', $pInfo->products_image); ?></td>
         </tr>

to

          <tr>
           <td class="main"><?php echo TEXT_PRODUCTS_IMAGE; ?></td>
           <td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . tep_draw_file_field('products_image'); ?></td>
         </tr>
         <tr>
           <td class="main"><?php echo TEXT_PRODUCTS_IMAGE_DESTINATION; ?></td>
           <td class="main"><?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . tep_draw_input_field('image_destination', $pInfo->products_image); ?></td>
         </tr>

Around line 887, change from

        $contents[] = array('text' => '<br>' . TEXT_CATEGORIES_IMAGE . '<br>' . tep_draw_file_field('categories_image'));

to

        $contents[] = array('text' => '<br>' . TEXT_CATEGORIES_IMAGE . '<br>' . tep_draw_file_field('categories_image'));
       $contents[] = array('text' => '<br>' . TEXT_CATEGORIES_IMAGE_DESTINATION . '<br>' . tep_draw_input_field('image_destination'));

Around line 905, change from

        $contents[] = array('text' => '<br>' . TEXT_EDIT_CATEGORIES_IMAGE . '<br>' . tep_draw_file_field('categories_image'));

to

        $contents[] = array('text' => '<br>' . TEXT_EDIT_CATEGORIES_IMAGE . '<br>' . tep_draw_file_field('categories_image'));
       $contents[] = array('text' => '<br>' . TEXT_CATEGORIES_IMAGE_DESTINATION . '<br>' . tep_draw_input_field('image_destination', $cInfo->categories_image));

In admin/includes/languages/english/categories.php (or whatever language), add something like

  define('TEXT_CATEGORIES_IMAGE_DESTINATION', 'File location in the images directory: ');
 define('TEXT_PRODUCTS_IMAGE_DESTINATION', 'File location in the images directory: ');

 

 

add in admin/includes/configure.php

 

// BOF define your ftp connection

define('FTP_SERVER', '12.34.567.890'); // FTP server IP, same info as you use to upload by a FTP client

define('FTP_SERVER_USERNAME', 'login'); // same login info as you use to upload by a FTP client

define('FTP_SERVER_PASSWORD', 'password'); // same password as you use to upload by a FTP client

define('DIR_FTP_MKDIR', '/httpdocs/catalog/images'); // login by your FTP client to see the path you need

// EOF define your ftp connection

 

 

create a new file and call it: ftpmkdir.php

put this code block into the file and save it, then upload it to your admin/includes/ftpmkdir.php

 

<?php
/* Created by Robert Hellemans with information from php dot net
NOTE: ONLY use this if your admin directory is protected from outside !!!
ftpmkdir.php 12 Dec 2003 Robert Hellemans
webmaster at RuddlesMills dot com
Feel FREE to use this file but leave my information in it
Thank you
*/
// create directory through FTP connection
function FtpMkdir($path, $newDir) {
      $connection = ftp_connect(FTP_SERVER); // connection

      $result = ftp_login($connection, FTP_SERVER_USERNAME, FTP_SERVER_PASSWORD); // login to ftp server

  // check if connection was made
    if ((!$connection) || (!$result)) {
      return false;
      exit();
      } else {
        ftp_chdir($connection, $path); // go to destination dir
      if(ftp_mkdir($connection,$newDir)) { // create directory
	 ftp_site($connection, "CHMOD 777 $path/$newDir") or die("FTP SITE CMD failed.");
          return $newDir;
      } else {
          return false;
      }
  ftp_close($conn_id); // close connection
  }

}
?>

 

 

The person who started this as a TIP & TRICK is Matt (iiinetworks)

Thank you Matt, i gave it a little swing to let other people work some more on this to make it even better then it is already


Robert

 

We all need to learn it once, how hard it may seem when you look at it, also you will master it someday ;)

Share this post


Link to post
Share on other sites

/me kisses Robert and Matt on the cheek!!

 

Oh I love you both!

 

Thank you SO much for this!

 

Khim~


Do not meddle in the affairs of Dragons, for you are crunchy and good with ketchup :-)

Share this post


Link to post
Share on other sites

Help!!

 

I got this message after installing the mods:

 

Warning:  ftp_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known (is your IPV6 configuration correct? If this error happens all the time, try reconfiguring PHP using --disable-ipv6 option to configure) in /home/tubbytabbygifts.com/html/admin/includes/ftpmkdir.php on line 11

 

Warning:  ftp_login() expects parameter 1 to be resource, boolean given in /home/tubbytabbygifts.com/html/admin/includes/ftpmkdir.php on line 14

 

IVP6???

 

Oh help me :-)

 

Khim~


Do not meddle in the affairs of Dragons, for you are crunchy and good with ketchup :-)

Share this post


Link to post
Share on other sites
Help!!

 

I got this message after installing the mods:

 

Warning:? ftp_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known (is your IPV6 configuration correct? If this error happens all the time, try reconfiguring PHP using --disable-ipv6 option to configure) in /home/tubbytabbygifts.com/html/admin/includes/ftpmkdir.php on line 11

 

Warning:? ftp_login() expects parameter 1 to be resource, boolean given in /home/tubbytabbygifts.com/html/admin/includes/ftpmkdir.php on line 14

 

IVP6???

 

Oh help me :-)

 

Khim~

Allright here is a testfile upload it to /home/tubbytabbygifts.com/html/ftp_test.php

and start it with http://www.tubbytabbygifts.com/ftp_test.php

 

Then check by your ftp client if the directory /home/tubbytabbygifts.com/html/ftp_test is made, this can be programmed too but i dont want to go there right now ;)

 

Let me know by PM (private messaging) to keep this board from cluther...

 

<?php
// create directory through FTP connection
/* by Robert Hellemans
replace:
xx.xx.xxx.xxx  => FTP IP address
   ftp_login       => FTP login name
   ftp_password	=> FTP password
*/
function FtpMkdir($path, $newDir) {
      // $path = DIR_FS_CATALOG_IMAGES
      // $newDir = $HTTP_POST_VARS['image_destination']
      $ftp_server='xx.xx.xxx.xxx'; // ftp server IP
      $connection = ftp_connect($ftp_server); // connection

      // login to ftp server
      $user = "ftp_login";
      $pass = "ftp_password";
      $result = ftp_login($connection, $user, $pass);

  // check if connection was made
    if ((!$connection) || (!$result)) {
      return false;
      exit();
      } else {
        ftp_chdir($connection, $path); // go to destination dir
      if(ftp_mkdir($connection,$newDir)) { // create directory
           // issue SITE command...
	 ftp_site($connection, "CHMOD 777 $path/$newDir") or die("FTP SITE CMD failed.");
          return $newDir;
      } else {
          return false;
      }
  ftp_close($conn_id); // close connection
  }

}
// the next line should make a directory in /home/tubbytabbygifts.com/html/ftp_test
// if its there then let me know
FtpMkdir("/httpdocs", "ftp_test");
?>


Robert

 

We all need to learn it once, how hard it may seem when you look at it, also you will master it someday ;)

Share this post


Link to post
Share on other sites

I have another question about this mod.

 

In admin, where you set the path to your image and browse for the file, how do you input the path? What is the format?

 

For example:

 

If I want to upload foo.gif, I browse for foo.gif (I'm on a Mac) and choose it.

Next I need to set where in /images I want it to go... so I type in products/foo

 

I get "directory is not writeable" (although it is) and it doesn't show the image. When I right click to open my non-existant image in a new window... I get a lovely 404 error.

 

:blink:

 

My /images dir is chmod 777 so what am I doing wrong?

 

I'm sorry to be such a pain :(

 

Khim~


Do not meddle in the affairs of Dragons, for you are crunchy and good with ketchup :-)

Share this post


Link to post
Share on other sites

What i did was just select the file to upload

Products Image: (browse the file to upload)

File location in the images directory: products_small/foo.jpg

 

you NEED to add the directoryname BEFORE the foo.jpg (like i did in GREEN text)

 

i have installed linda's mcgraths (?) SECGV etc.. contribution with extra images and building in this new contribution 'we' created as we speak...


Robert

 

We all need to learn it once, how hard it may seem when you look at it, also you will master it someday ;)

Share this post


Link to post
Share on other sites

sorry time out while editing above post....

 

 

What i did was just select the file to upload

Products Image: (browse the file to upload)

File location in the images directory: products_small/foo.jpg

 

you NEED to add the directoryname BEFORE the foo.jpg (like i did in GREEN text)

This will create the directory in images/products_small/

when it already exists it will let you know if you have uncommented this:

see code block Around lines 315-20, change from

 

// uncomment the next line if you want to debug

// $messageStack->add(DIR_WS_IMAGES . $image_destination_dir[0] . " already exists", 'caution');

 

i have installed linda's mcgraths (?) SECGV etc.. contribution with extra images and building in this new contribution 'we' created as we speak...


Robert

 

We all need to learn it once, how hard it may seem when you look at it, also you will master it someday ;)

Share this post


Link to post
Share on other sites

I use Lee Nielsen's Ultrapics contribution which changes the image uploading code. I can't change anything after "Around lines 562-5, change from" because it's not there. Any chance of a Ultrapics compliant version?

Share this post


Link to post
Share on other sites

Sorry, my work is done on this contribution.

It's Time to make new contributions, or time for you to learn PHP... (j/k)


Robert

 

We all need to learn it once, how hard it may seem when you look at it, also you will master it someday ;)

Share this post


Link to post
Share on other sites

It took me a little while to figure out the correct changes and paths, but I finally made it! Great contribution! Works like a charm! :)

 

Just a little hint for noobs like me: on admin/includes/configure.php, add the path like that:

define('DIR_FTP_MKDIR', '/public_html/catalog/images'); // login by your FTP client to see the path you need

and don't forget to change your FTP server, username and password.

 

Man, this will save me a lot of work uploading new products or editing old ones. Cool! :D

 

Thanks to all of you who made this possible. It should be incorporated to OSC!

Edited by Patty

Patty

Share this post


Link to post
Share on other sites

Would it be possible to create deeper subcategories level? I have products going all the way to subcategory 4 or 5 (ie. images/subcat1/subcat2/subcat3/subcat4/image.jpg), but I'm getting error warnings (Error: Destination does not exist.) when trying to upload the images.

 

TIA for your help. :)


Patty

Share this post


Link to post
Share on other sites

*****************

Would it be possible to create deeper subcategories level? I have products going all the way to subcategory 4 or 5 (ie. images/subcat1/subcat2/subcat3/subcat4/image.jpg), but I'm getting error warnings (Error: Destination does not exist.) when trying to upload the images.

*********************

 

change the field properties in table products->product_images use phpadmin or mysqladmin : the vachar is 64 change 128 as you one..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×