I have written PHP script to upload 1000 products in magento .

But here is the problem .Script only run for 40 seconds and upload 4 or 5 products and then it mySQL time out .

So my server does not allow the scrip to run for that much longer period .

So how can I run the script after few minutes to prevent timeout errors . I can provide you the code as well .


$row = 1;
if (($handle = fopen("butler.csv", "r")) !== FALSE) {

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        $product = Mage::getModel('catalog/product');
        $num = count($data);

        if ($data[12]) {
            $b = str_replace('$', '', $data[12]);
        } else {
            $b = str_replace('$', '', $data[11]);

        $category = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', $data[14]);
        $cat_det = $category->getData();
        $category_id = $cat_det[0][entity_id];

        $c = 10;
        $imgnum = sprintf("%07d", $data[0]);

        if ($row >= 29 && $row <= 100) {

            try {

                    ->setData('brand', $data[2])
                    ->setData('keyattribute', $data[3])
                    ->setData('collectionname', $data[13])
                    ->setData('finishname', $data[17])
                    ->setData('upc_code', $data[15])
                    ->setData('productdimension', $data[16])
                    ->setData('materials', $data[19])
                    ->setData('stylecategory', $data[21])
                    ->setData('color', $data[22])
                    ->setData('shape', $data[23])
                        'use_config_manage_stock' => 0, // use global config?
                        'manage_stock' => 1, // should we manage stock or not?
                        'is_in_stock' => 1,
                        'Qty' => 5))
                    ->setName($data[4])// add string attribute
                    ->setDescription($data[1] . '<br>' . $data[20])
                    ->setShortDescription($data[1] . '<br>' . $data[20])

                $image_url = 'http://www.butlerspecialtyfurniture.net/resources/low-res/' . $imgnum; //get external image url from csv

                $image_type = substr(strrchr($image_url, "."), 1); //find the image extension
                $filename = $imgnum . '.jpg'; //give a new name, you can modify as per your requirement
                $filepath = Mage::getBaseDir('media') . DS . 'import' . DS . $filename; //path for temp storage folder: ./media/import/
                file_put_contents($filepath, file_get_contents(trim($image_url))); //store the image from external url to the temp storage folder
                $mediaAttribute = array(

                $product->addImageToMediaGallery($filepath, $mediaAttribute, false, false)
                    ->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
                    ->addImageToMediaGallery($filepath, array('image', 'thumbnail', 'small_image'), false, false); //assigning image, thumb and small image to media gallery

                echo 'Row#' . $row . '-' . $data[0] . '-Uploaded' . '\n';
            } catch (Exception $e) {
                echo 'Caught exception: ', $e->getMessage(), "\n";



closed as off-topic by Raphael at Digital Pianism, Qaisar Satti, Keyul Shah, Marius Apr 1 '16 at 6:42

  • This question does not appear to be about the Magento e-Commerce platform within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    I'm voting to close this question as off-topic because it is a PHP only question and is not related to Magento. – Raphael at Digital Pianism Mar 30 '16 at 14:56
  • 1
    Surely you just need to increase the execution time limit? php.net/manual/en/function.set-time-limit.php – Alex Mar 30 '16 at 14:57
  • 1
    Raphael I am adding products in Magento .So it is related to magento as well .. – Adil Mar 30 '16 at 19:41
  • 1
    @Alex .No the php limit is not the solution .I need to stop my call to server and then start it again . – Adil Mar 30 '16 at 19:43
  • Can you run it as a CRON job every few minutes and upload at most 4 products at a time? You will need to keep track of which products (from butler.csv) have been uploaded and upload the next batch... etc. (Although this does seem to be very slow?) – MrWhite Mar 30 '16 at 20:42

If you hit a mysql timeout the problem is that your script is doing too much between two queries, therefore you can just close the connection after your query and open a new one for the next query.

If you hit a php timeout (max_execution_time error) then you should stay with @alex tipp and increate the max_execution_time or run from CLI which has a default max_execution_time of 0 (run forever)

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