繁体   English   中英

文件名包含空格的问题

[英]Issue with filename containing spaces

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

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

例如:考虑

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); @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.php264

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

    结果:

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

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

    结果:

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

  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请求失败! 在第265行的/var/www/html/opencart3/admin/model/account/apisync.php


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

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

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

如果有任何其他解决方案可以解决这种情况?

使用此代码 不要使用空格

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

https://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