I have found a script that adds a 'sample request' button that should allow me to add a sample of every product, I'm getting an error when I click the sample request button.

This is the error I get:

Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Image does not exist.' in /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/Mage.php:595 Stack trace: #0 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php(274): Mage::throwException('Image does not ...') #1 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/code/core/Mage/Catalog/Model/Product.php(1047): Mage_Catalog_Model_Product_Attribute_Backend_Media->addImage(Object(Mage_Catalog_Model_Product), '/var/www/vhosts...', Array, false, false) #2 /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/getSample.php(82): Mage_Catalog_Model_Product->addImageToMediaGallery('/var/www/vhosts...', Array, false, false) #3 {main} thrown in /var/www/vhosts/webcontrolcentre.co.uk/httpdocs/perfume/app/Mage.php on line 595

The script that allows the sample to be created is in a file called getSample.php and it includes:


 ini_set('display_errors', 1);


 require_once 'app/Mage.php';

 Mage::getSingleton("core/session", array("name" => "frontend"));
 // Store users session
 $session = Mage::getSingleton("customer/session");

 //Store cart items
 $totalNumOfCartItem = Mage::getModel('checkout/cart')->getItemsQty();

 //Store cart items
 $theitems = Mage::getModel('checkout/cart')->getItems();

 $thecount = 0;

 foreach($theitems as $ti){

 $thename = $ti['name'];

 $test = strpos($thename,'Free Sample');

 if($test !== false){
 $thecount = $thecount+1;


 $name = $_POST['name'];

 $id = $_POST['id'];
 if(isset($_POST['simpleid']) && !empty($_POST['simpleid'])){
 $simpleid = $_POST['simpleid'];
 $name = $_GET['name'];
 if(isset($_GET['simpleid']) && !empty($_GET['simpleid'])){
 $simpleid = $_GET['simpleid'];
 $id = $_GET['id'];

 // retrict shopping cart to only allow 5 samples
 if ($totalNumOfCartItem <= 4)
 $theproduct = Mage::getModel('catalog/product')->load($id);

 $thesimple = Mage::getModel('catalog/product')->load($simpleid);

 if($thecount < 2)
 // instatiate Product
 $product = Mage::getModel('catalog/product');
 } else {
 $product->setName("Sample of ".$name);
 // $product->setDescription("Sample of ".$name);
 $product->setShortDescription($name." – Sample");

 $product->setImageUrl($theproduct->getImageUrl()); // set the images from the original product
 $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
 $product->addImageToMediaGallery($_SERVER['DOCUMENT_ROOT']."/media/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false);
 $product->setAttributeSetId(9); // need to look this up
 $product->setCategoryIds("15"); // need to look these up
 $product->setTaxClassId(2); // taxable goods
 $product->setVisibility(1); // catalog, search
 $product->setStatus(1); // enabled
 // assign product to the default website

 // get product’s general info such price, status, description

 $stockData = $product->getStockData();

 // update stock data using new data
 $stockData['qty'] = 1;
 $stockData['is_in_stock'] = 1;
 $stockData['manage_stock'] = 1;
 $stockData['max_sale_qty'] = 5;

 // then set product’s stock data to update

 // call save() method to save your product with updated data

 header("Location: /checkout/cart/add/product/".$product->getId()."/");

 Mage::getSingleton('checkout/session')->addError("You are only allowed a maximum of 5 sample products");

 header("Location: /checkout/cart");

I'm hoping someone can help as I really would like an 'add sample' button but cannot find a solution anywhere so I'm hoping I can get this script to work.


1 Answer 1


You would better use Mage::getBaseDir('media') instead of $_SERVER['DOCUMENT_ROOT']."/media in your addImage function. If things remain the same, try check what is the path to the image and if the image exists there.

  • Thanks for the reply, this maybe a stupid question but how would I use the Mage::getbaseDir('media') in this line of code?$product->addImageToMediaGallery($_SERVER['DOCUMENT_ROOT']."/media/catalog/product".$theproduct->getSmallImage(), array('image','small_image','thumbnail'), false, false); I've tried a couple of ways but still getting the error and the image does exist in that file directory.
    – Duggy G
    Commented Oct 13, 2014 at 18:11
  • you replace $_SERVER['DOCUMENT_ROOT']."/media by Mage::getBaseDir('media')." Commented Oct 13, 2014 at 18:23
  • I have done that and now I get a different error which is a Uncaught exception 'PDOException, so I think your solution has fixed the first error, thankyou for your help.
    – Duggy G
    Commented Oct 13, 2014 at 18:34
  • can you tick as resolved ? Commented Oct 13, 2014 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.