Search code examples
phpmagentomagento-soap-api

How To Specify Filename When Uploading Product Picture Using The Magento Soap Api


I'm using the Magento Soap Api to upload pictures of products. I can't seem to specify the name I wish Magento so save the file as on the server even though I specify the "name" as per the Magento documentation.

I'm using Magento 1.2.1.2 and can't upgrade as the site uses modules that break in newer version.

Any thoughts on how I can specify the file name that Magento saves the image as?

The code I'm using:

$client->call(
    $session_id,
    'catalog_product_attribute_media.create',
    array(
      $sku,
      array(
        'file' => array(
          'content' => base64_encode(file_get_contents($filename)),
          'mime' => $imageinfo['mime'],
          'name' => $filename
        ),
        'label' => $description,
        'types' => array(
          'image',
          'small_image',
          'thumbnail'
        ),
        'exclude' => FALSE
      )
    )
  );

Solution

  • You can't specify the filename. The only solution would be for you change the Magento files to allow a filename as suggested in this thread. For your convenience I've included the solution below (and adapted it based on the fact that you're sending the filename as name above):

    Modify ./app/code/core/Mage/Catalog/Model/Product/Attribute/Media/Api.php:

    You'll want to replace $fileName = 'image.' . $this->_mimeTypes[$data['file']['mime']]; with:

    if ( ! empty($data['file']['name']) )
      $fileName = $data['file']['name'];
    else
      $fileName = 'image.' . $this->_mimeTypes[$data['file']['mime']];
    

    If you feel a little more adventurous, you could extend the class instead of changing it and override the create function with your own functionality.