【问题标题】:file_exists() return false for files with whitespaces in the name对于名称中带有空格的文件,file_exists() 返回 false
【发布时间】:2015-05-27 23:32:13
【问题描述】:

我有这个代码:

$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

有什么建议吗?

【问题讨论】:

  • 您的空间可能需要 urlencoding
  • 这听起来很不错!让我测试一下
  • 那么或许你需要考虑PHP docsTip - As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality”中的注释,注意http://不支持stat()
  • servImagesDir 应该是$servImagesDir
  • 不,这是我的配置图像路径.. 问题出在远程图像中,而不是在本地服务器上.. 测试您是否可以复制此图像然后打开它..

标签: php file-exists


【解决方案1】:

问题在于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?

【讨论】:

  • 这是在远程服务器上,我无权访问它。在我的情况下如何检查文件?
  • 另外我正在使用 copy() 函数来复制文件,但它也找不到文件
  • #CBroe 这个问题现在有了新的维度:)。
【解决方案2】:

试试这个:

copy('http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg', '/image.jpeg');

如果没有,请使用file_get_contents

//Get the file
$content = file_get_contents("http://www.bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);

【讨论】:

  • #Ahmed,你能不能试试用你的方法复制那个图片,看你复制成功但是图片打不开,你试试看好吗?
  • 我忘了添加这个,'/image.jpeg' 你现在可以试试吗?:copy('bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg', '/image.jpeg');
  • 同样的事情,你也试过了吗?你能看到/打开图片吗?
  • 试试这个并向我们展示错误:copy('bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg', 'image.jpg');用“http://www.”
  • 警告:复制(bgelectronics.eu/image/cache/data/cantell kabeli /202441-500x500.jpg):无法打开流:/www/...中没有这样的文件或目录/...../root/example2.php 第 18 行
猜你喜欢
  • 2015-09-14
  • 2017-08-15
  • 2013-10-12
  • 2018-12-15
  • 2012-01-28
  • 2013-07-04
  • 2012-09-02
  • 2018-10-31
相关资源
最近更新 更多