2012-01-13 49 views
0

我有简单的CI(CodeIgniter)代码。我想要做的是:当我进入图片路径textarea的,PHP下载每张图片到我的服务器,并给他们的文件名,但我有这些错误:file_get_contents给我失败打开流

Message: file_get_contents(http://geosmiley.ge/Upload/Product/42/1.jpg) [function.file-get-contents]: failed to open stream: Invalid argument 

Message: file_get_contents(http://geosmiley.ge/Upload/Product/42/2.jpg) [function.file-get-contents]: failed to open stream: Invalid argument 

这里是我的CI代码:

$image = explode("\n", $_POST['images']); 
for ($i = 0; $i < count($image); $i++){ 
    if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){ 
     file_put_contents('images/'.$title_url.'_'.$i.'.jpg', file_get_contents($image[$i])); 
     $image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';         
    } 
} 
$poster = $image[0]; 
$images = implode("\n", $image); 

它能做什么是简单的下载最后文件,并为第一和第二文件给我的错误

请不要给我其他方面的建议喜欢使用的卷曲FOPEN。我认为这是正确的方式,因为PHP成功下载LAST FILE。请帮我解决我的问题

+1

是 “allow_fopen_url” 开启? http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen – 2012-01-13 21:16:02

+0

“Tandu”我不会允许fopen,出于安全目的 – Irakli 2012-01-13 21:20:01

+0

那么你不能使用' file_get_contents'。它还要求打开设置。 – 2012-01-13 21:25:47

回答

1

我不知道为什么发生这个问题,但这里是修复它的代码,这要感谢“NikiC”谁发现了witespace“”是问题,我解决了它。

此代码不能正常工作

$image = explode("\n", $_POST['images']); 
for ($i = 0; $i < count($image); $i++){ 
    if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){ 
     file_put_contents('images/'.$title_url.'_'.$i.'.jpg', file_get_contents($image[$i])); 
     $image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';         
    } 
} 
$poster = $image[0]; 
$images = implode("\n", $image); 

这是工作代码

$image = explode("\n", $_POST['images']); 
for ($i = 0; $i < count($image); $i++){ 
    if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){ 
     if ($i < count($image)-1){ 
      $kk = substr($image[$i], 0, strlen($image[$i]) - 1); 
     } else { 
      $kk = $image[$i]; 
     } 
     if ($this->download($kk, 'images/'.$title_url.'_'.$i.'.jpg')){ 
      $image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg'; 
     }else{ 
      echo($image[$i]); 
     } 
    } 
} 
//**************************************************** 
    public function download($url, $path){ 
     if (file_put_contents($path, file_get_contents($url))){return TRUE;}else{return FALSE;} 
    } 
0

把你的URL换成引号;使它成为一个字符串

+0

看起来像你是对的,但我很好奇,因为他给了一个变量file_get_contents($ image [$ i])的url,为什么需要引号? – 2012-01-13 21:17:25

+0

我试过file_get_contents('''。$ image [$ i]。'“');但没有工作 – Irakli 2012-01-13 21:19:27

+0

请看看我修改过的代码,可以帮助你,帮助我,plzz – Irakli 2012-01-14 11:27:29

3

manual

的URL可以当的fopen 包装已启用作为具有这种功能的文件名。

您将需要设置allow_url_fopen为true或使用cURL代替。

+0

我想知道为什么我的代码不工作?我做了什么错误 – Irakli 2012-01-13 21:23:03

+0

你需要检查你的php.ini中的'allow_url_fopen'设置。如果没有正确设置,'file_get_contents'将会失败。 – webbiedave 2012-01-13 21:26:22

+0

@ user939421 as webbiedave提到它可能在您的服务器端被禁用,请检查您的phpinfo或使用cURL扩展 – Nazariy 2012-01-13 21:26:36

相关问题