file_get_contents给我失败打开流
我有简单的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。请帮我解决我的问题
我不知道为什么发生这个问题,但这里是修复它的代码,这要感谢“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;}
}
我想知道为什么我的代码不工作?我做了什么错误 – Irakli 2012-01-13 21:23:03
你需要检查你的php.ini中的'allow_url_fopen'设置。如果没有正确设置,'file_get_contents'将会失败。 – webbiedave 2012-01-13 21:26:22
@ user939421 as webbiedave提到它可能在您的服务器端被禁用,请检查您的phpinfo或使用cURL扩展 – Nazariy 2012-01-13 21:26:36
是 “allow_fopen_url” 开启? http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen – 2012-01-13 21:16:02
“Tandu”我不会允许fopen,出于安全目的 – Irakli 2012-01-13 21:20:01
那么你不能使用' file_get_contents'。它还要求打开设置。 – 2012-01-13 21:25:47