B
B
Borysenko2020-10-01 21:19:06
PHP
Borysenko, 2020-10-01 21:19:06

PHP code freezes, cycle stops working?

There is an XML file for 3000 products. It has a table with categories, each has its own id and parent_id. Each product is assigned a category_id and a category_name. But the products have only their current category written without the parent hierarchy. And I wrote such a code at the output of which we get for each product in the category category > sub_category > sub_sub category and so on. This whole thing works. But the problem is that it only works with a small file, and when I try to take the whole or even half, it reaches 30% and hangs stupidly. The page keeps loading but the code doesn't execute

<?php
set_time_limit(0);                   // ignore php timeout
ignore_user_abort(true);             // keep on going even if user pulls the plug*
while(ob_get_level())ob_end_clean(); // remove output buffers
ob_implicit_flush(true);             // output stuff directly

ini_set('memory_limit','4000M');


if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK) {
    $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
    $fileName = $_FILES['uploadedFile']['name'];
    $fileSize = $_FILES['uploadedFile']['size'];
    $fileType = $_FILES['uploadedFile']['type'];
    $fileNameCmps = explode(".", $fileName);
    $fileExtension = strtolower(end($fileNameCmps));

    $newFileName = md5(time() . $fileName) . '.' . $fileExtension;

    $uploadFileDir = './uploaded_files/';
    $dest_path = $uploadFileDir . $newFileName;
    
    if(move_uploaded_file($fileTmpPath, $dest_path))
    {
        $message ='File is successfully uploaded.';

        $file = fopen($dest_path, 'r');

        $xmlstr = file_get_contents($dest_path);
        unlink($dest_path);

        $products_list = new SimpleXMLElement($xmlstr);

        echo "<pre>";
        
        $categories = [];
        foreach($products_list->xpath('categories') as $category):
            foreach($category->xpath('category') as $category):
                $categories[] = $category;
            endforeach;
        endforeach;
        var_dump(count($products_list->products_list->product));
        $processed = 0;
        $progress = 0;
        foreach ($products_list->xpath('products_list') as $product) :
            $products = $product->xpath('product');
            $all_count = count($products) ;
            foreach ($products as $product) :

                $cats = [];

                $parent_category = intval($product->category_id);
                
                while($parent_category != 0):
                    
                    foreach($categories as $category):
                        
                        if(intval($category->id) == $parent_category):
                            $cats[] = trim(strval($category->name));
                            $parent_category = intval($category->parent_id);
                        endif;
                        
                    endforeach;
                endwhile;

                $categories_output = "";
                $i = count($cats);
                while($i >= 0){
                    $categories_output.= $cats[$i];
                    if($i != 0 && $i != count($cats)): $categories_output.=" > "; endif;
                    $i--;
                }
                $product->category_name = $categories_output;
                // echo $categories_output ."<br>";
                $processed++;
                $cur_progress = intval($processed/$all_count * 100);
                if($progress !=  intval($cur_progress)){
                    $progress = intval($cur_progress);
                    echo $progress."%<br>";
                }  
                // var_dump($progress);
                // die();
             
            endforeach;
            
        endforeach;
        
        echo "</pre>";
                $file = "list-2.xml";
                
                if ($products_list->asXML($file)) {
                    echo 'Saved!<br>';
                } else {
                    echo 'Unable to save to file :(';
                };   
        // // Открываем файл в нужном нам режиме. Нам же, нужно его создать и что то записать.
        // $file = "sks_list-2.xml";
        
        // if ($products_list->asXML($file)) {
        //     echo 'Saved!';
        // } else {
        //     echo 'Unable to save to file :(';
        // };
    }
    else
    {
      $message = 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
    }
}


Here is my processing code. Exposed in php.ini 4000M and timeout 9999 but still hangs. Tell me what to do about it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2020-10-01
@Borysenko

SimpleXML is not suitable for large files. For them, it is better to use XMLReader, which does not build the entire tree in memory, but reads the nodes one at a time. You read categories and products from the file, then you compare one with another in pure Pyhe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question