EDITED
I am loading product by cron, this function reads whole dir, gets all files names i.e test_16_008_NL.png here test_16_008_NL is sku of product, then it loads add entries in tables catalog_product_entity_media_gallery , catalog_product_entity_media_gallery_value and catalog_product_entity_varchar, like all entries by uploading manually from admin. Product image is being uploaded, can be view from backed, Also present in db and in disk in folder media >> catalog >> product, but not appearing on front end. I have checked folder permission, deleted cache, indexed data and deleted .htaccess file under media folder, what I am missing?
CASE
Image is appeared on front if I go to admin and open a product i.e test_16_008_NL product just have to save.Otherwise image is not appearing.My store is multiple store and uploded file look like this
my cron call is
public function importImagesFromDisk() {
$filename = array();
$sourcePath = Mage::getBaseDir('media') . DS . 'import' . DS;
if (is_dir($sourcePath)) {
if ($dir = opendir($sourcePath)) {
while (($value1 = readdir($dir)) !== false) {//reading whole directory
$filename [] = $value1;
}
closedir($dir);
}
}
$productUpdate = array();
$productFile = array();
foreach ($filename as $fName) {
if (pathinfo($fName, PATHINFO_EXTENSION)) {//is it a file
$name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fName); //Drop extension of file
$_productId = Mage::getModel('catalog/product')->getIdBySku($name);
if ($_productId) {
$productUpdate[$_productId] = 'Insert';
$productFile[$_productId] = $fName;
}
}
}
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$mediaGalleryAttributeId = $installer->getAttribute($entityTypeId, 'media_gallery', 'attribute_id'); //Get attribute_id of attribute "media_gallery"
$smallImageAttributeId = $installer->getAttribute($entityTypeId, 'small_image', 'attribute_id'); //Get attribute_id of attribute "small_image"
$storeId = Mage::app()->getStore('admin')->getId(); //As admin store is used for uploading images
$read = Mage::getSingleton('core/resource')->getConnection('core_read'); // To read from the database
$cpevTable = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_varchar');
$query = "SELECT value_id, value, entity_id FROM " . $cpevTable . " WHERE attribute_id= " . $smallImageAttributeId;
$result = $read->query($query); //Execute query
//Fetch row one by one from result
while ($row = $result->fetch()) {
$fields = array();
$fields['value'] = $row['value'];
if (array_key_exists($row['entity_id'], $productUpdate)) {
$productUpdate[$row['entity_id']] = $row['value_id'];
}
}
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); // For writing into database
$write->beginTransaction();
foreach ($productUpdate as $productID => $update) {
$file = $productFile[$productID];
$fileName = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file); //Drop extension of file
$parentFolder = $fileName[0];
$childFolder = $fileName[1] ? $fileName[1] : '_';
$destinationPath = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . DS . $parentFolder . DS . $childFolder . DS;
if (!file_exists($destinationPath)) {
//Creating folder with write permission
mkdir($destinationPath, 0777, true);
}
try {
if (copy($sourcePath . $file, $destinationPath . $file)) {//Copy file from $source location to destination
$imagePath = DS . $parentFolder . DS . $childFolder . DS . $file; //Path used in db to refer image
$mediaGalleryFields = array();
$mediaGalleryFields['attribute_id'] = $mediaGalleryAttributeId;
$mediaGalleryFields['entity_id'] = $productID;
$mediaGalleryFields['value'] = $imagePath;
$write->insert('catalog_product_entity_media_gallery', $mediaGalleryFields);
$write->commit();
$lastInsertId = $write->fetchOne('SELECT last_insert_id()');
$mediaGalleryValueFields = array();
$mediaGalleryValueFields['value_id'] = $lastInsertId;
$mediaGalleryValueFields['store_id'] = $storeId;
// $mediaGalleryValueFields['label'] = $imagePath;//By default Null will be placed
$mediaGalleryValueFields['position'] = 1;
$mediaGalleryValueFields['disabled'] = 0;
$write->insert('catalog_product_entity_media_gallery_value', $mediaGalleryValueFields);
$write->commit();
$catalogProductFields = array();
$catalogProductFields['entity_type_id'] = $entityTypeId;
$catalogProductFields['attribute_id'] = $smallImageAttributeId;
$catalogProductFields['store_id'] = $storeId;
$catalogProductFields['entity_id'] = $productID;
$catalogProductFields['value'] = $imagePath;
if ($update != 'Insert') {
$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$where = $write_connection->quoteInto('value_id =?', $update);
$write_connection->update('catalog_product_entity_varchar', $catalogProductFields, $where);
} else {
$write->insert('catalog_product_entity_varchar', $catalogProductFields);
$write->commit();
}
$_product = Mage::getModel('catalog/product')->load($productID);
$_product->save();
unlink($sourcePath . $file); //Delete file from source directory
}
} catch (Exception $e) {
echo 'Error Message: ' . $e->getMessage();
}
}
Mage::getModel('catalog/product_image')->clearCache(); //Clear images Cache
}