简体   繁体   English

PHP为什么不读取该文件?

[英]Why won't PHP read this file?

I'm trying to make it retrieve the image files on the server but it won't work if there is a space in the name of the image file .. for example there is a space between dead and air , even if I escape it after adding %20, the function returns an empty string .. but if it is a file with no space in the name like ' http://www.m.trialsite.com/images/thumb/Espresso.jpg '; 我正在尝试使其检索服务器上的图像文件,但是如果图像文件名中有空格,则该行将不起作用。例如,即使我逃脱了,死角和空气之间也有空格添加%20之后,该函数将返回一个空字符串..但如果该文件的名称中没有空格,例如“ http://www.m.trialsite.com/images/thumb/Espresso.jpg ”; It will work ! 会工作的! .. where am I going wrong ? ..我要去哪里错了?

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

}

use preg_replace("/\\s+/","_",$nome); 使用preg_replace("/\\s+/","_",$nome); to rename the files and then recovers it it will work 重命名文件,然后恢复它会起作用

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

如果您这样做是怎么办的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM