【问题标题】:file_get_contents gives me failed to open streamfile_get_contents 让我无法打开流
【发布时间】:2012-01-13 21:12:14
【问题描述】:

我有简单的 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。请帮我解决我的问题

【问题讨论】:

  • “allow_fopen_url”是否开启? php.net/manual/en/…
  • "Tandu" 为了安全起见,我不会允许 fopen
  • 那么您不能使用带有file_get_contents 的网址。它还需要打开该设置。
  • 请看我修改后的代码,可以帮到你,plzz

标签: php arrays url download


【解决方案1】:

来自manual

如果 fopen 包装器已启用。

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

【讨论】:

  • 我想知道为什么我的代码不起作用?我犯了什么错误
  • 您需要检查您的 php.ini 中的 allow_url_fopen 设置。如果设置不正确,file_get_contents 将失败。
  • @user939421 as webbiedave 提到它可能在您的服务器端被禁用,检查您的 phpinfo 或使用 cURL 扩展
  • 请看我修改后的代码,可以帮到你,plzz
  • 按照 webbiedave 的说明进行操作。制作一个只有&lt;?=phpinfo();?&gt; 的PHP 脚本运行它,并检查是否设置了allow_url_fopen
【解决方案2】:

我不知道为什么会出现问题,但这里是修复它的代码,感谢“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;}
    }

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 看起来你是对的,但我很好奇,因为他给出了带有变量 file_get_contents($image[$i]) 的 url,为什么需要引号?
    • 我试过 file_get_contents('"'.$image[$i].'"');但没有工作
    • 请看我修改后的代码,可以帮到你,plzz
    猜你喜欢
    • 1970-01-01
    • 2020-10-02
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多