【问题标题】:Issue with filename containing spaces文件名包含空格的问题
【发布时间】:2020-01-28 02:56:38
【问题描述】:

OpenCart 框架版本 3.0.2.0 中存在错误,

如果存在文件名之间包含空格的文件,则处理和执行时间过长

例如:考虑

img = https://DomainNameServer/image/catalog/pimages/SKU081985 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;
}

具有以下这些功能:

  1. 图像以 Corrupted/RAW 格式保存,延迟时间更长,没有吞吐量 file_put_contents($dstfile, $return);

  2. 图像在适当的吞吐量下完美保存,延迟更长 copy(str_replace(" ", "%20", $srcfile), $dstfile);

  3. 图像以文本格式保存,延迟较长且吞吐量不正确 @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);

    结果:

    警告:复制(https%3A%2F%2Fwww.DomainNameServer.in%2Fimage%2Fcatalog%2Fpimages%2FSKU093126+%281%29.jpg): 无法打开流:中没有这样的文件或目录 /var/www/html/opencart3/admin/model/account/apisync.php上线 264

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

    结果:

    警告:copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126 (1).jpg):打开流失败:HTTP 请求失败!在 /var/www/html/opencart3/admin/model/account/apisync.php264

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

    结果:

    警告:copy(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126(1).jpg): 无法打开流:HTTP 请求失败!在 /var/www/html/opencart3/admin/model/account/apisync.php上线 265

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

    结果:

    警告: 复制(https://www.DomainNameServer.in/image/catalog/pimages/SKU093126(1).jpg): 无法打开流:HTTP 请求失败!在 /var/www/html/opencart3/admin/model/account/apisync.php上线 265


在构建 API 功能时,在 10,000 种产品中,只有 1091 种产品保存到数据库中,其余 8909 种产品需要 7 到 10 小时 执行时间长。由于我已将执行时间限制设置为无限。 10小时后我检查时,所有产品都成功保存。

总时间与否成正比。包含空格的图像文件名

导出编号。产品的名称根据包含空格的图像文件名而有所不同。 当我调试三个图像文件名带空格的产品时,如果没有 addSubImages() 函数,它需要 20 分钟才能完成,它甚至不需要一秒钟的时间来执行

是否有任何其他方法可以解决这种情况?

【问题讨论】:

    标签: php image opencart opencart-3 image-file


    【解决方案1】:

    使用此代码 不要使用空间

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

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

    【讨论】:

    • 试过了,没用。因为它们是一回事
    • 你都看过了吗?
    【解决方案2】:

    修改 Prasannas 答案这对我有用:

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

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2014-07-05
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2013-11-18
      相关资源
      最近更新 更多