Error import CSV file Crontab - php

I created a script to import a CSV file. When I run it by hand with the php command on the server the script runs without problems. On the other hand when the crontab execute it there is an error:
SPLFILEOBJECT::__CONSTRUCT(IMPORTS/STOCK_EXPRESS_FR.CSV): FAILED TO
OPEN STREAM: NO SUCH FILE OR DIRECTORY .
Here is my import script.
<?php
require(dirname(__FILE__) . '/config/config.inc.php');
$dir_fichier = 'IMPORTS/STOCK_EXPRESS_FR.csv';
//error_reporting(E_ALL);
ini_set('display_errors', 0);
tntProduct($dir_fichier);
function tntProduct($dir_fichier)
{
$errors = [];
try{
$csv = new SplFileObject($dir_fichier);
$csv->setFlags(SplFileObject::READ_CSV);
$csv->setCsvControl(';');
} catch (Exception $exception) {
$errors[] = $exception->getMessage();
}
$nbProducts = 0;
$nbProductsSuccess = 0;
foreach ($csv as $ligne) {
if ($ligne[1] === '0') {
$result = getIdsProducts($ligne[0]);
if (!empty($result)) {
foreach ($result as $key => $item) {
$nbProducts++;
try {
setCarrier($item['id_product']);
$nbProductsSuccess++;
}catch (Exception $exception) {
$errors[] = $exception->getMessage();
}
}
}
}
}
if (count($errors)>0) {
sendMail($errors);
} else {
sendMail($nbProducts);
}
if (file_exists ($dir_fichier)) {
unlink($dir_fichier);
}
}
function getIdsProducts($ref)
{
$sql = Db::getInstance()->executeS('
SELECT p.id_product
FROM ' . _DB_PREFIX_ . 'product p
WHERE p.reference = ' . '"' . $ref . '"');
return $sql;
}
function setCarrier($productId)
{
$shop = 1;
$carriersFR = [
0 => 1,
1 => 72,
2 => 87
];
$products = new Product((int)$productId, false, 1, $shop);
$products->setCarriers($carriersFR);
}
function sendMail($result)
{
$data['{message}'] = $result;
Mail::Send(
1,
'tnt_fr',
'Mise à jour TNT France',
$data,
'test#test.fr',
'test',
null,
null,
null,
true,
_PS_MAIL_DIR_,
false,
1
);
}
Do you have an idea of ​​the problem ? Thank you for your help

In crontab run the script, is not be in the folder, so you need to write full path or correct relative path.

Related

List Azure files and folders from File share snapshots with php

To list the files and folders from Azure Files can be done with this code:
function ListFolder($shareName, $path)
{
global $fileRestProxy;
$vMaxResultados = 5000;
$vNextMarker = "";
$listResult = null;
try
{
do
{
$options = new ListDirectoriesAndFilesOptions();
$options->setMaxResults($vMaxResultados);
$options->setMarker($vNextMarker);
$listResult = $fileRestProxy->ListDirectoriesAndFiles($shareName,$path,$options);
$vNextMarker = $listResult->getNextMarker();
} while ($vNextMarker != "");
}
catch (Exception $e)
{
$code = $e->getCode();
$error_message = $e->getMessage();
return "ERROR:$code:$error_message";
}
return $listResult;
}
But how is the sintaxis or method to the same with a snapshot from these Share?
This doesn't work:
function ListSnapshotFolder($shareName, $path, $snapshot)
{
global $fileRestProxy;
$vMaxResultados = 5000;
$vNextMarker = "";
$listResult = null;
try
{
do
{
$options = new ListDirectoriesAndFilesOptions();
$options->setMaxResults($vMaxResultados);
$options->setMarker($vNextMarker);
$shareFull = $shareName . "?snapshot=" . $snapshot;
$listResult = $fileRestProxy->ListDirectoriesAndFiles($shareFull,$path,$options);
$vNextMarker = $listResult->getNextMarker();
} while ($vNextMarker != "");
}
catch (Exception $e)
{
$code = $e->getCode();
$error_message = $e->getMessage();
return "ERROR:$code:$error_message";
}
return $listResult;
}
Is there any parameter in the $option object to add?
Or maybe the $shareFull must be created in some format?
$shareFull = $shareName . "?snapshot=" . $snapshot;
Thanks in advance.
I believe you have found a bug in the SDK. I looked up the source code here and there's no provision to provide sharesnapshot query string parameter in the options as well as the code does not even handle it.
public function listDirectoriesAndFilesAsync(
$share,
$path = '',
ListDirectoriesAndFilesOptions $options = null
) {
Validate::notNull($share, 'share');
Validate::canCastAsString($share, 'share');
Validate::canCastAsString($path, 'path');
$method = Resources::HTTP_GET;
$headers = array();
$postParams = array();
$queryParams = array();
$path = $this->createPath($share, $path);
if (is_null($options)) {
$options = new ListDirectoriesAndFilesOptions();
}
$this->addOptionalQueryParam(
$queryParams,
Resources::QP_REST_TYPE,
'directory'
);
$this->addOptionalQueryParam(
$queryParams,
Resources::QP_COMP,
'list'
);
$this->addOptionalQueryParam(
$queryParams,
Resources::QP_PREFIX_LOWERCASE,
$options->getPrefix()
);
$this->addOptionalQueryParam(
$queryParams,
Resources::QP_MARKER_LOWERCASE,
$options->getNextMarker()
);
$this->addOptionalQueryParam(
$queryParams,
Resources::QP_MAX_RESULTS_LOWERCASE,
$options->getMaxResults()
);
$this->addOptionalQueryParam(
$queryParams,
Resources::QP_TIMEOUT,
$options->getTimeout()
);
$dataSerializer = $this->dataSerializer;
return $this->sendAsync(
$method,
$headers,
$queryParams,
$postParams,
$path,
Resources::STATUS_OK,
Resources::EMPTY_STRING,
$options
)->then(function ($response) use ($dataSerializer) {
$parsed = $dataSerializer->unserialize($response->getBody());
return ListDirectoriesAndFilesResult::create(
$parsed,
Utilities::getLocationFromHeaders($response->getHeaders())
);
}, null);
}
You may want to open up an issue here: https://github.com/Azure/azure-storage-php/issues and bring this to SDK team's attention.

Convert procedural code into class for Blobs

So I have the following code that I built out that grabs some images from an Azure storage library:
$accountName = 'teststorageaccount';
$accountKey = '**';
$containerName = 'users';
$connectionString = "DefaultEndpointsProtocol=http;AccountName={$accountName};AccountKey={$accountKey}";
$blobClient = ServicesBuilder::getInstance()->createBlobService($connectionString);
try {
$blob_list = $blobClient->listBlobs($containerName);
$blobs = $blob_list->getBlobs();
// Grab all the blob links
foreach ($blobs as $blob) {
echo $blob->getUrl() . "</br>";
}
} catch(ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Then I'm getting the following results back:
http://teststorageaccount.blob.core.windows.net/users/ABREUG.jpg
http://teststorageaccount.blob.core.windows.net/users/ABUKHADA.jpg
http://teststorageaccount.blob.core.windows.net/users/ACHANT.jpg
http://teststorageaccount.blob.core.windows.net/users/ACQUISTE.jpg
Now this is where I would need some help .. I'm practicing on building out everything in a class, and this is how far I've come:
class AzureStorage
{
private $accountName;
private $accountKey;
private $containerName;
public static function init()
{
return new AzureStorage([
'accountName' => 'teststorageaccount',
'accountKey' => '***',
'containerName' => 'users',
]);
}
/**************************************************************************/
public function __construct(array $data = [])
{
if (count($data) === 0) {
return;
}
$this->load($data);
}
public function load(array $data) : void
{
if (isset($data['accountName'])) {
$this->accountName = $data['accountName'];
}
if (isset($data['accountKey'])) {
$this->accountKey = $data['accountKey'];
}
if (isset($data['containerName'])) {
$this->containerName = $data['containerName'];
}
}
public function connect()
{
$connectionString = "DefaultEndpointsProtocol=https;AccountName={$this->accountName};AccountKey={$this->accountKey}";
$blobClient = ServicesBuilder::getInstance()->createBlobService($connectionString);
return $blobClient;
}
public function getContainers() : array
{
$containers = $this->connect()->listContainers();
return $containers->getContainers();
}
public function getBlobURLs()
{
try {
$blob_list = $this->connect()->listBlobs($this->containerName);
$blobs = $blob_list->getBlobs();
// Grab all the blob links
foreach ($blobs as $blob) {
echo $blob->getUrl() . "</br>";
}
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
}
}
The problem: How would I be able to use the try and catch inside the getBlobURLs method and then output the results? I'm unable to get results when calling it outside the class.
Here is what I'm doing:
$test2 = new \AzureStorage\AzureStorage([
'accountName' => 'teststorageaccount',
'accountKey' => '***',
'containerName' => 'users',
]);
Now if I call the following (I get an array of containers, which works perfectly):
$containers = $test2->getContainers();
//var_dump($containers);
But if I do the following (I get no results outputted back):
$blobs = $test2->getBlobURLs();
var_dump($blobs);
Does anyone know why I might not be getting the URLs back?
You do have a return statement in getContainers function but not in getBlobURLs.
public function getBlobURLs(){
try {
$blob_list = $this->connect()->listBlobs($this->containerName);
$blobs = $blob_list->getBlobs();
$blobUrls = [];
// Grab all the blob links
foreach ($blobs as $blob) {
$blobUrls[] = $blob->getUrl();
}
return $blobUrls;
} catch (ServiceException $e) {
return false;
}
}
Now if you want to display blob url list then
$blobs = $test2->getBlobURLs();
if($blobs === false){
echo 'Error while getting blob urls.';
}
else{
foreach($blobs as $blobUrl){
echo $blobUrl . "</br>";
}
}

Amazon Product Advertising API 5.0 with SDK php return error 500

In a list of Amazon products that I retrieve by analyzing the WordPress database, I have this code that allows me to get product by product their Amazon information:
function get_product_infos(asin, ext){
$.post(window.location.href, {
activate: 'get-product-informations',
asin: asin,
ext: ext
}, function(data){
//var json = JSON.parse(data);
console.log(data);
/*if(json.response === 'alright'){
var current_asin = json.current_asin;
current_asin = $('#'+current_asin);
var next_asin = current_asin.next().attr('id');
var next_ext = current_asin.next().attr('data-domain-ext');
current_asin.find('td').first().find('.progress').text('ok');
if(typeof next_asin !== typeof undefined && next_asin !== false){
get_product_infos(next_asin, next_ext);
}
}*/
});
}
File called with $.post() to have the product information from Amazon:
<?php
$autorisation = isset($_POST['activate']) ? $_POST['activate'] : '';
$current_asin = isset($_POST['asin']) ? $_POST['asin'] : '';
$current_ext = isset($_POST['ext']) ? $_POST['ext'] : '';
if($autorisation !== 'get-product-informations'){
return;
}
use pa\src\com\amazon\paapi5\v1\api\DefaultApi;
use pa\src\ApiException;
use pa\src\Configuration;
use pa\src\com\amazon\paapi5\v1\GetItemsRequest;
use pa\src\com\amazon\paapi5\v1\GetItemsResource;
use pa\src\com\amazon\paapi5\v1\PartnerType;
use pa\src\com\amazon\paapi5\v1\ProductAdvertisingAPIClientException;
require_once(plugin_dir_url( __FILE__ . '/bundles/admin-pages/tool/crawling-system/pa/vendor/autoload.php' ));
function parseResponse($items){
$mappedResponse = array();
foreach ($items as $item) {
$mappedResponse[$item->getASIN()] = $item;
}
return $mappedResponse;
}
function getItems(){
global $wpdb;
$prefix_table = $wpdb->prefix;
$table_name = $prefix_table.'azon_lc';
$result = ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name) ? $wpdb->get_results("SELECT * FROM $table_name") : '';
$access_key = ($result !== '') ? $result[0]->access_key : '';
$secret_key = ($result !== '') ? $result[0]->secret_key : '';
$associate_tag = ($result !== '') ? $result[0]->associate_tag : '';
$config = new Configuration();
/*
* Add your credentials
* Please add your access key here
*/
$config->setAccessKey($access_key);
# Please add your secret key here
$config->setSecretKey($secret_key);
# Please add your partner tag (store/tracking id) here
$partnerTag = $associate_tag;
/*
* PAAPI host and region to which you want to send request
* For more details refer: https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
*/
$config->setHost('webservices.amazon.'.$current_ext);
$config->setRegion('us-east-1');
$apiInstance = new DefaultApi(
/*
* If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
* This is optional, `GuzzleHttp\Client` will be used as default.
*/
new pa\vendor\GuzzleHttp\guzzle\src\Client(), $config);
# Choose item id(s)
$itemIds = array($current_asin);
/*
* Choose resources you want from GetItemsResource enum
* For more details, refer: https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter
*/
$resources = array(
GetItemsResource::ITEM_INFOTITLE,
GetItemsResource::OFFERSLISTINGSPRICE);
# Forming the request
$getItemsRequest = new GetItemsRequest();
$getItemsRequest->setItemIds($itemIds);
$getItemsRequest->setPartnerTag($partnerTag);
$getItemsRequest->setPartnerType(PartnerType::ASSOCIATES);
$getItemsRequest->setResources($resources);
# Validating request
$invalidPropertyList = $getItemsRequest->listInvalidProperties();
$length = count($invalidPropertyList);
if ($length > 0) {
echo "Error forming the request", PHP_EOL;
foreach ($invalidPropertyList as $invalidProperty) {
echo $invalidProperty, PHP_EOL;
}
return;
}
# Sending the request
try {
$getItemsResponse = $apiInstance->getItems($getItemsRequest);
echo 'API called successfully', PHP_EOL;
echo 'Complete Response: ', $getItemsResponse, PHP_EOL;
# Parsing the response
if ($getItemsResponse->getItemsResult() != null) {
echo 'Printing all item information in ItemsResult:', PHP_EOL;
if ($getItemsResponse->getItemsResult()->getItems() != null) {
$responseList = parseResponse($getItemsResponse->getItemsResult()->getItems());
foreach ($itemIds as $itemId) {
echo 'Printing information about the itemId: ', $itemId, PHP_EOL;
$item = $responseList[$itemId];
if ($item != null) {
if ($item->getASIN()) {
echo 'ASIN: ', $item->getASIN(), PHP_EOL;
}
if ($item->getItemInfo() != null and $item->getItemInfo()->getTitle() != null
and $item->getItemInfo()->getTitle()->getDisplayValue() != null) {
echo 'Title: ', $item->getItemInfo()->getTitle()->getDisplayValue(), PHP_EOL;
}
if ($item->getDetailPageURL() != null) {
echo 'Detail Page URL: ', $item->getDetailPageURL(), PHP_EOL;
}
if ($item->getOffers() != null and
$item->getOffers()->getListings() != null
and $item->getOffers()->getListings()[0]->getPrice() != null
and $item->getOffers()->getListings()[0]->getPrice()->getDisplayAmount() != null) {
echo 'Buying price: ', $item->getOffers()->getListings()[0]->getPrice()
->getDisplayAmount(), PHP_EOL;
}
} else {
echo "Item not found, check errors", PHP_EOL;
}
}
}
}
if ($getItemsResponse->getErrors() != null) {
echo PHP_EOL, 'Printing Errors:', PHP_EOL, 'Printing first error object from list of errors', PHP_EOL;
echo 'Error code: ', $getItemsResponse->getErrors()[0]->getCode(), PHP_EOL;
echo 'Error message: ', $getItemsResponse->getErrors()[0]->getMessage(), PHP_EOL;
}
} catch (ApiException $exception) {
echo "Error calling PA-API 5.0!", PHP_EOL;
echo "HTTP Status Code: ", $exception->getCode(), PHP_EOL;
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
if ($exception->getResponseObject() instanceof ProductAdvertisingAPIClientException) {
$errors = $exception->getResponseObject()->getErrors();
foreach ($errors as $error) {
echo "Error Type: ", $error->getCode(), PHP_EOL;
echo "Error Message: ", $error->getMessage(), PHP_EOL;
}
} else {
echo "Error response body: ", $exception->getResponseBody(), PHP_EOL;
}
} catch (Exception $exception) {
echo "Error Message: ", $exception->getMessage(), PHP_EOL;
}
}
getItems();
exit;?>
I tested the file in several ways to understand when there was an error and I determined that by trying to use this code it crashes:
$config = new Configuration();
I would like to determine why the call returns a 500 error? There is obviously something that I do not understand.

ajax request returns an unexpected output with the correct output

This is the output I get from ajax request to php pdo:
[{"sys_id":"1","task":"qwe","task_date":"11\/30\/2017 8:49 PM","task_person":"qwe","task_status":"0"},{"sys_id":"2","task":"asd","task_date":"11\/30\/2017 9:54 PM","task_person":"asd","task_status":"0"}]null
As shown there is an excess null value which I cant figure out where it is coming from my code is:
function selecttask(action) {
$.ajax({
type: 'POST',
url: '../include/demo.php',
dataType: "json",
data: {
action: action
},
success: function(data) {
}
}).done(function(data) {
});
}
selecttask("selectall");
My demo.php is:
<?php
include_once("crud.php");
//include_once("../config/config.php");
//$con = new connect_pdo();
$crud = new Crud();
$action = $_POST['action'];
$data = $_POST['data'];
switch (strtolower($action)):
case("selectall"):
$table = "list_tbl";
$selectall = $crud->selectall($table);
echo json_encode($selectall, JSON_UNESCAPED_UNICODE);
break;
case("add"):
$table = "list_tbl";
$insert = $crud->insert($table,$data);
echo json_encode($insert, JSON_UNESCAPED_UNICODE);
break;
endswitch;
?>
Then crud is:
<?php
include_once("../config/config.php");
class Crud extends connect_pdo {
public $_con;
function __construct() {
parent::__construct();
$this->_con = $this->dbh();
}
public function selectall($table_name) {
$queryselectall = "Select * from {$table_name}";
$selectall = $this->_con->prepare($queryselectall);
if ($selectall->execute()) {
if ($selectall->rowCount() > 0) {
$result = $selectall->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
}else{
echo array('error'=> TRUE, 'message'=> 'No result found.');
}
}
}
public function insert($table_name, $res) {
parse_str($res, $data);
$limit = count($data);
$ctr = 0;
$columns = "";
$values = "";
foreach ($data as $key => $value) {
$ctr++;
$columns.= "{$key}";
$values .= ":{$key}";
if ($ctr < $limit) {
$columns.= ",";
$values .= ",";
}
}
$query = "INSERT INTO {$table_name} ({$columns})VALUES({$values})";
try {
$create = $this->_con->prepare($query);
foreach ($data as $key => $value) {
$keys = ":{$key}";
$create->bindValue($keys, $value, PDO::PARAM_STR);
}
if ($create->execute()) {
$lastinserted_id = $this->_con->lastInsertId();
echo array('error' => FALSE, 'message' => 'Data added successfully.', 'lastinserted_id' => $lastinserted_id, 'query' => $query);
} else {
echo array('error' => TRUE, 'message' => 'Execution failed, please contact system support!');
}
} catch (Exception $ex) {
echo array('error' => TRUE, 'message' => $ex);
}
}
}
?>
From the above code I cant determine where the null is coming from.
Did I miss something that is why null is coming as result of ajax request
Probably a better architecture for your Crud class to handle DB interactions by running the queries and returning the results as an array (or throwing an exception if anything exceptional happens). Then your demo.php script can just get the array from the Crud class method and handle the output (json encoding, response output).

PHP script is running out of memory

I wrote the following PHP script (symphony 1.4 Task), which loops over a CSV file an d imports the data via Doctrine into MySQL.
Unfortunately the script is aborted because it allocates to much memory. The used memory grows with each foreach cycle. I tried a lot of things, but i am not able to keep the used memory more stable.
Could anyone give me a hint?
<?php
class importERPTask extends sfBaseTask
{
protected function configure()
{
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
// add your own options here
));
$this->namespace = 'w2';
$this->name = 'importERP';
$this->briefDescription = 'Imports product data .';
}
public function execute($arguments = array(), $options = array())
{
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
$this->logSection('importERP', 'Start');
if(!(file_exists(sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'VK.csv') && file_exists(sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'Artikel.csv')))
{
$this->logSection('importERP', 'No import CSV');
$this->logSection('importERP', 'Done.');
return;
}
$this->importPrices();
//import products
$this->logSection('importERP', 'Import products');
Doctrine::getTable('CatalogueProduct')->setAllImportFalse();
$file_handle = fopen(sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'Artikel.csv', 'r');
if($file_handle)
{
$i = 0;
while(!feof($file_handle))
{
//skip first line
if(++$i == 1)
{
continue;
}
$this->importProduct($file_handle);
}
fclose($file_handle);
}
$this->deleteProducts();
unlink(sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'VK.csv');
unlink(sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'Artikel.csv');
$this->logSection('importERP', 'Done.');
}
private function importPrices()
{
//import price helper table
$this->logSection('importERP', 'Import price helper table');
Doctrine::getTable('ImportHelperPrice')->clearAllData();
$file_handle = fopen(sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'VK.csv', 'r');
if($file_handle)
{
$i = 0;
while(!feof($file_handle))
{
$line_of_text = fgetcsv($file_handle, 0, ';', '"');
//skip first line
if(++$i == 1)
{
continue;
}
$price = new ImportHelperPrice();
$price->setImportId($line_of_text[0]);
$price->setStartAmount($line_of_text[1]);
$price->setPrice(str_replace(',', '.', $line_of_text[2]));
$price->save();
}
}
}
private function importProduct($file_handle)
{
$line_of_text = fgetcsv($file_handle, 0, ';', '"');
$this->logSection('importERP', 'Import product '.$line_of_text[1]);
//no empty article number
if($line_of_text[0] == '')
{
$this->logSection('importERP', '... skipped');
return;
}
if($line_of_text[4] == '')
{
$this->logSection('importERP', '... has no category');
return;
}
$my_product = Doctrine::getTable('CatalogueProduct')->findOneByCode($line_of_text[0]);
$my_cat = Doctrine::getTable('CatalogueCategory')->findOneByImportCode($line_of_text[4]);
if(!$my_cat)
{
$this->logSection('importERP', '... has no category');
return;
}
if(!$my_product)
{
$this->logSection('importERP', '... is new');
$my_product = new CatalogueProduct();
$my_product->setCode($line_of_text[0]);
// do not overwrite handmade configurations from backend
$my_product->setVatId(1);
$my_product->setTemplateId(4);
}
else
{
$this->logSection('importERP', '... is updated');
}
//get prices
$price = Doctrine::getTable('ImportHelperPrice')->getPriceForImportId($line_of_text[0]);
if(!$price)
{
return;
}
$my_product->setPriceGrossEur($price->getPrice());
$my_product->Translation['de']->title = $line_of_text[2];
$my_product->Translation['de']->shortdescription = $line_of_text[3];
$my_product->Translation['de']->description =$line_of_text[3];
$my_product->setCatalogueCategory($my_cat);
$my_product->setHidden(false);
$my_product->setImportFlag(true);
$config_prices = Doctrine::getTable('ImportHelperPrice')->getPriceConfigForImportId($line_of_text[0]);
if($config_prices)
{
$price_config = '';
foreach($config_prices as $cp)
{
$discount = 100 - ($cp->getPrice() / ($price->getPrice() / 100));
if($discount == 0)
{
continue;
}
if($price_config != '')
{
$price_config .= ';';
}
$price_config .= $cp->getStartAmount() . ':' . number_format($discount, 2, ',', '');
}
$my_product->setPriceConfig($price_config);
}
//move images
$img_default = sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$line_of_text[1].'_m.jpg';
if(file_exists($img_default))
{
rename($img_default, sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR.$line_of_text[1].'_m.jpg');
$my_product->setImageDefault($line_of_text[1].'_m.jpg');
$this->logSection('importERP', '... '.$my_product->getImageDefault().' saved');
}
else
{
$this->logSection('importERP', '... '.$img_default.' not found');
}
$img_zoom = sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$line_of_text[1].'_gr.jpg';
if(file_exists($img_zoom))
{
rename($img_zoom, sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR.$line_of_text[1].'_zoom.jpg');
$my_product->setImageZoom($line_of_text[1].'_zoom.jpg');
$this->logSection('importERP', '... '.$my_product->getImageZoom().' saved');
}
else
{
$this->logSection('importERP', '... '.$img_zoom.' not found');
}
$img_icon = sfConfig::get('sf_root_dir'). DIRECTORY_SEPARATOR .'import_data'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$line_of_text[1].'_kl.jpg';
if(file_exists($img_icon))
{
rename($img_icon, sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR.$line_of_text[1].'_icon.jpg');
$my_product->setImageIcon($line_of_text[1].'_icon.jpg');
$this->logSection('importERP', '... '.$my_product->getImageIcon().' saved');
}
else
{
$this->logSection('importERP', '... '.$img_icon.' not found');
}
$my_product->save();
$this->logSection('importERP', 'Memory usage '.memory_get_peak_usage() / 1048576,2 .' MB');
}
private function deleteProducts()
{
//delete not mentioned products
$this->logSection('importERP', 'Delete not mentioned products');
$del_products = Doctrine::getTable('CatalogueProduct')->getAllImportFalse();
foreach($del_products as $dp)
{
$this->logSection('importERP', 'Delete '.$dp->getCode());
//delete images
$img_default = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR.$dp->getImageDefault();
$img_zoom = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR.$dp->getImageZoom();
$img_icon = sfConfig::get('sf_web_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR.$dp->getImageIcon();
if($dp->getImageDefault() != NULL && $dp->getImageDefault() != '' && file_exists($img_default))
{
unlink($img_default);
}
if($dp->getImageZoom() != NULL && $dp->getImageZoom() != '' && file_exists($img_zoom))
{
unlink($img_zoom);
}
if($dp->getImageIcon() != NULL && $dp->getImageIcon() != '' && file_exists($img_icon))
{
unlink($img_icon);
}
//delete product
$dp->delete();
}
}
}
The best solution would be to edit your php.ini file. Allocate:
memory_limit = 16M
Not sure if 16M is default but it should be close. Anyway, that's the amount of memory a PHP script is allowed to use. You can just set this number higher and solve the problem.
Alternatively you can write this on the top of your PHP script:
ini_set('memory_limit', '16M');
Where 16M can be changed to whatever amount of memory you wish the script to allow.
Watch out for ini_sets with values ending with 'M' - they don't work as expected. Shorthand notation works only in php.ini, appropriate info is included in the docs: http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes
You should do:
...
$my_product->save();
$my_product->free();
unset($my_product);
...
Assuming your CSV is just too big for your server to handle in memory in one swoop, I would consider splitting the CSV into smaller files, which is quite a simple task with Unix commands (split, etc).
You would then execute each bitesize CSV asynchronously with your PHP script.

Categories