file_get_contents让我无法打开流

时间:2012-01-13 21:12:14

标签: php arrays url download

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

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的cURL。我认为这是正确的方式因为php成功下载了LAST FILE。请帮我解决我的问题

3 个答案:

答案 0 :(得分:3)

来自manual

  

如果fopen,URL可以用作此函数的文件名   包装器已启用。

您需要将allow_url_fopen设置为true或改为使用cURL

答案 1 :(得分:1)

我不知道为什么问题发生了但是这里修复了它的代码,感谢“NikiC”发现问题空间“”是问题,我修复了它。

此代码不起作用

$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;}
    }

答案 2 :(得分:0)

将您的网址用引号括起来;把它变成一个字符串

相关问题