我有这个代码:
$url = 'http://www.bgelectronics.eu/image/cache/data/cantell kabeli /14222-228x228.jpg';
$headers = get_headers($url, true);
var_dump($headers);
由于文件名中存在空格,这会返回丢失文件的错误:
array(6) { [0]=> string(34) "HTTP/1.1 500 Internal Server Error" ["Date"]=>
string(29) "Tue, 24 Mar 2015 16:11:18 GMT" ["Server"]=> string(6) "Apache"
["Content-Length"]=> string(3) "677" ["Connection"]=> string(5) "close"
["Content-Type"]=> string(29) "text/html; charset=iso-8859-1" } file size:677
请问有什么建议吗?
最佳答案
问题在于file_exists()
是针对文件系统的,而不是针对http的。您需要使用服务器目录路径。如果它与您的代码位于同一服务器上,则它应该看起来像:
if(file_exists('image/cache/data/cantell kabeli /202441-500x500.jpg')){
....
如果在远程服务器上,请尝试:
if(file_get_contents('http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg')){
...
您可以在这里找到许多其他方法:How can one check to see if a remote file exists using PHP?
关于php - file_exists() 对于名称中包含空格的文件返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236299/