为什么PHP不会读取此文件?

时间:2013-12-20 18:26:52

标签: php php-5.3 fread

我正在尝试让它检索服务器上的图像文件,但如果图像文件的名称中有空格则无法工作..例如,死空间之间有空格,即使我在添加%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; 

}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

如果你这样做会怎么样:

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