0

There is a bug in an OpenCart Framework version 3.0.2.0,

If there are files with the filename containing spaces between them, it takes too long to process and execute

For Eg: Consider

img = https://DomainNameServer/image/catalog/pimages/SKU 081985 P80.jpg

<?php

set_time_limit(0);
ignore_user_abort(true);

public  function addSubImages($images){    
      $Image =array();

      foreach($images['img'] as $key => $img){

          $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
          $headers[] = 'Connection: Keep-Alive';         
          $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
          $user_agent = 'php';         
          $process = curl_init($img); // http://localhost/bluemb/image/catalog/pimages/SKU 081985 P80.jpg    
          curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
          curl_setopt($process, CURLOPT_HEADER, 0);         
          curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
          curl_setopt($process, CURLOPT_TIMEOUT, 1800);   
          curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 0); 
          //curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 1700); 
          curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
          curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
          $return = curl_exec($process);         
          curl_close($process);         
          //return $return;  

          $filepath  = pathinfo($img);
          $dirname  = DIR_IMAGE.'catalog/prod/';

          if (!file_exists($dirname)) {
            mkdir($dirname, 0755, true);
          } 

          if (!empty($dirname)) {
            $srcfile = $img;
            $dstfile = DIR_IMAGE.'catalog/prod/'.$filepath['basename']; // /var/www/html/opencart/image/catalog/prod/SKU 081985 P80.jpg
            $Image[] = 'catalog/prod/'.$filepath['basename'];  //catalog/prod/SKU 081985 P80.jpg
            copy(str_replace(" ","%20",$srcfile), $dstfile);
            //file_put_contents($dstfile,$return);
          } 
          else {
            $Image = "";
          }

      }      
  return $Image;
}

With these below functions:

  1. The images were saved in Corrupted/RAW format with more latency without a throughput file_put_contents($dstfile, $return);

  2. The images were saved perfectly with more latency with proper throughput copy(str_replace(" ", "%20", $srcfile), $dstfile);

  3. The images were saved in Text format with more latency with improper throughput @copy($srcfile, $dstfile); $content = file_get_contents($srcfile); $fp = fopen($dstfile , "w+"); fwrite($fp, $content); $Image = 'catalog/prod/'.$filepath['basename']; fclose($fp); curl_close($process);

  4. copy(urlencode($srcfile), $dstfile);

    Results:

    Warning: copy(https%3A%2F%2Fwww.DomainNameServer.in%2Fimage%2Fcatalog%2Fpimages%2FSKU093126+%281%29.jpg): failed to open stream: No such file or directory in /var/www/html/opencart3/admin/model/account/apisync.php on line 264

  5. copy(urldecode($srcfile), $dstfile);

    Results:

    Warning: copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): failed to open stream: HTTP request failed! in /var/www/html/opencart3/admin/model/account/apisync.php on line 264

  6. copy(str_replace('_','%20',$srcfile), $dstfile);

    Results:

    Warning:copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): failed to open stream: HTTP request failed! in /var/www/html/opencart3/admin/model/account/apisync.php on line 265

  7. copy(str_replace('%20',' ',$srcfile), $dstfile);OR copy(str_replace('%20','_',$srcfile), $dstfile);OR copy(str_replace('%20','',$srcfile), $dstfile);

    Results:

    Warning: copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): failed to open stream: HTTP request failed! in /var/www/html/opencart3/admin/model/account/apisync.php on line 265


When building the API functionality, out of 10,000 products only 1091 products are saving into a database and rest of 8909 products took 7 - 10 hours long to execute. Since I have set the execution time limit to Infinite. When I checked after 10 hours, all the products were successfully saved.

Total Time Taken is directly proportional to no. of Image File name containing spaces

Exporting the no. of the product varies according to Image File name containing spaces. While when I was debugging for three products having the image file name with spaces, it was taking 20 mins in order complete without the addSubImages() function it took not even a single second to execute

If there are any other approaches for the solutions in order to get around this scenario?

Nɪsʜᴀɴᴛʜ ॐ
  • 2,756
  • 4
  • 33
  • 57

2 Answers2

0

Use this code Dont use space

copy(str_replace("%20", "", $srcfile), $dstfile);
OR 
copy(str_replace("%20", "_", $srcfile), $dstfile);

See https://www.w3schools.com/php/func_string_str_replace.asp

0

Modifying Prasannas answer this worked for me:

$string_without_space = str_replace(" ", "_", $string_with_space);