简体   繁体   中英

Issue with filename containing spaces

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); @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:

    : copy(https%3A%2F%2Fwww.DomainNameServer.in%2Fimage%2Fcatalog%2Fpimages%2FSKU093126+%281%29.jpg): failed to open stream: No such file or directory in on line :复制(https%3A%2F%2Fwww.DomainNameServer.in%2Fimage%2Fcatalog%2Fpimages%2FSKU093126+%281%29.jpg):无法打开流: 没有这样的文件或目录

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

    Results:

    : copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): failed to open stream: HTTP request failed!:复制(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg):无法打开流:HTTP请求失败! in on line 行的

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

    Results:

    :copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): failed to open stream: HTTP request failed!:copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): 无法打开流:HTTP 请求失败! in on line 行的

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

    Results:

    : copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): failed to open stream: HTTP request failed!:复制(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg):无法打开流:HTTP请求失败! in on line 行的


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?

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

修改 Prasannas 答案这对我有用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM