简体   繁体   中英

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 '; 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); 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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