简体   繁体   English

文件名包含空格的问题

[英]Issue with filename containing spaces

There is a bug in an OpenCart Framework version 3.0.2.0, OpenCart 框架版本 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 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);图像以 Corrupted/RAW 格式保存,没有吞吐量file_put_contents($dstfile, $return);

  2. The images were saved perfectly with more latency with proper throughput copy(str_replace(" ", "%20", $srcfile), $dstfile);通过适当的吞吐量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); @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警告:复制(https%3A%2F%2Fwww.DomainNameServer.in%2Fimage%2Fcatalog%2Fpimages%2FSKU093126+%281%29.jpg):无法打开流: /var/www/html/opencart3中没有这样的文件或目录/admin/model/account/apisync.php264

  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!警告:复制(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg):无法打开流:HTTP请求失败! in /var/www/html/opencart3/admin/model/account/apisync.php on line 264在第264行的/var/www/html/opencart3/admin/model/account/apisync.php

  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!警告:copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg): 无法打开流:HTTP 请求失败! in /var/www/html/opencart3/admin/model/account/apisync.php on line 265在第265行的/var/www/html/opencart3/admin/model/account/apisync.php

  7. copy(str_replace('%20',' ',$srcfile), $dstfile); OR copy(str_replace('%20','_',$srcfile), $dstfile);copy(str_replace('%20','_',$srcfile), $dstfile); OR copy(str_replace('%20','',$srcfile), $dstfile);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!警告:复制(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg):无法打开流:HTTP请求失败! in /var/www/html/opencart3/admin/model/account/apisync.php on line 265在第265行的/var/www/html/opencart3/admin/model/account/apisync.php


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.在构建 API 功能时,在 10,000 种产品中,只有 1091 种产品保存到数据库中,其余 8909 种产品需要 7 到 10 个小时才能执行。 Since I have set the execution time limit to Infinite.由于我已将执行时间限制设置为无限。 When I checked after 10 hours, all the products were successfully saved.当我在10个小时后检查时,所有产品都成功保存。

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当我调试三个带有空格的图像文件名的产品时,在没有addSubImages()函数的情况下,它需要 20 分钟才能完成,它甚至没有执行一秒钟

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.asphttps://www.w3schools.com/php/func_string_str_replace.asp

修改 Prasannas 答案这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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