包含空格的文件名问题

时间:2018-06-12 10:38:47

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

OpenCart Framework 3.0.2.0版中存在错误,

如果文件中的文件名包含空格,则处理和执行

需要很长时间

对于Eg:考虑

  

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;
}

具有以下功能:

  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.php 264

  6. 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

  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

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

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

    导出号码。产品的变化根据包含空格的图像文件名称而有所不同。 当我正在调试三个具有空格的图像文件名的产品时,它完成了20分钟而没有addSubImages()功能,甚至没有一秒执行

    如果解决方案还有其他方法可以绕过这种情况吗?

2 个答案:

答案 0 :(得分:0)

使用此代码 不要使用空间

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

请参阅https://www.w3schools.com/php/func_string_str_replace.asp

答案 1 :(得分:0)

修改Prasannas的答案对我有用:

$string_without_space = str_replace(" ", "_", $string_with_space);
相关问题