问题
I want to import some products images in 'media/import' directory from a external source url in magento 1.7 version. Someone please help me !
回答1:
Here is an example
$image_location = getDownloadImage("product",$image_url);
if ( file_exists($image_location) ) {
$product->addImageToMediaGallery($image_location,array('thumbnail','small_image','image'),true,false);
}
// Download Image
public function getDownloadImage($type,$file){
$path = str_replace("index.php","",$_SERVER["SCRIPT_FILENAME"]);
$import_location = $path.'media/catalog/';
if (!file_exists($import_location)){
mkdir($import_location, 0755);
}
$import_location = $path.'media/catalog/'.$type.'/';
if (!file_exists($import_location)){
mkdir($import_location, 0755);
}
// todo check if last character has /
$file_source = Mage::getStoreConfig('oscommerceimportconf/oscconfiguration/conf_imageurl',Mage::app()->getStore()).$file;
$file_target = $import_location."/".basename($file);
$file_path = "";
if (($file != '') and (!file_exists($file_target))){
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'wb');
if ($rh===false || $wh===false) {
// error reading or opening file
$file_path = "";
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
$file_path = $file_target;
}
}
fclose($rh);
fclose($wh);
}
if (file_exists($file_target)){
if ($type == 'category'){
$file_path = $file;
}else{
$file_path = $file_target;
}
}
return $file_path;
} }
回答2:
As I know default Magento import does not allow to import images from remote URLs. You need to download them from those URLS to 'media/import' directory at your FTP. Then to assign images to products in your Magento import file indicate image name for each products (with slash at the beginning - e.g. /imagename.jpg ). If you need to import images directly from URL without saving them first, you cay look for some script to do the task. Other way via extension. I know the one that allows to do it - Store Manager for Magento, but its paid.
来源:https://stackoverflow.com/questions/21545218/how-to-import-products-images-from-external-url-in-magento-1-7-0