簡體   English   中英

PHP為什么不讀取該文件?

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

我正在嘗試使其檢索服務器上的圖像文件,但是如果圖像文件名中有空格,則該行將不起作用。例如,即使我逃脫了,死角和空氣之間也有空格添加%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; 

}

使用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);
}

如果您這樣做是怎么辦的:

$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