【问题标题】:Why won't PHP read this file?为什么 PHP 不读取这个文件?
【发布时间】:2013-12-20 18:26:52
【问题描述】:

我正在尝试让它检索服务器上的图像文件,但如果图像文件的名称中有空格,它将不起作用..例如, dead 和 air 之间有空格,即使我在添加 %20 后对其进行了转义,该函数返回一个空字符串.. 但如果它是一个名称中没有空格的文件,例如 'http://www.m.trialsite.com/images/thumb/Espresso.jpg';它会起作用的! ..我哪里错了?

$filename = 'http://www.m.trialsite.com/images/thumb/dead air.jpg'; 



function readfile_chunked($filename,$retbytes=true) { 
   $chunksize = 1*(1024*1024); // how many bytes per chunk 
   $buffer = ''; 
   $cnt =0; 

   // $handle = fopen($filename, 'rb');
     $filename = str_replace(' ','%20',$filename); 
   $handle = fopen($filename, 'rb');  

   if ($handle === false) { 
       return false; 
   } 
   $filename = str_replace(' ','%20',$filename); 
   while (!feof($handle)) { 
       $buffer = fread($handle, $chunksize); 
       echo $buffer;  var_dump($buffer); exit; 
       ob_flush(); 
       flush(); 
       if ($retbytes) { 
           $cnt += strlen($buffer); 
       } 
   } 
       $status = fclose($handle); 
   if ($retbytes && $status) { 
       return $cnt; // return num. bytes delivered like readfile() does. 
   } 
   return $status; 

}

【问题讨论】:

  • 对远程文件使用本地文件操作是一个巨大的问题,并且有一些像这样完全不可预测和无法处理的怪异现象,我希望 PHP 永远不会实现这些包装器。使用 cURL。
  • 出于好奇:您可以直接在浏览器中打开文件吗?
  • 是的,我可以在浏览器上看到它..

标签: php php-5.3 fread


【解决方案1】:

使用preg_replace("/\s+/","_",$nome);重命名文件,然后恢复它就可以了

$directory = '/public_html/testfolder/';//example
if ($handle = opendir($directory)) { 
    while (false !== ($fileName = readdir($handle))) {     
        $newName = preg_replace("/\s+/","_",$fileName); 
        rename($directory . $fileName, $directory . $newName);
    }
    closedir($handle);
}

【讨论】:

  • 我不允许更改服务器中的文件名
  • 它适用于所有其他图像,除非有空间并且我认为我不能更改检索图像的方式,这不是个人项目。
【解决方案2】:

如果你这样做会怎样:

$filename = str_replace(' ','%20', 'http://www.m.trialsite.com/images/thumb/dead air.jpg');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多