将远程图像保存到本地服务器Codeigniter


Save Remote Image to local server Codeigniter

我试图在Codeigniter中使用Phils Curl脚本将远程图像保存到本地服务器。

为了确保目录在那里有适当的权限,我有:

if (!is_dir('images')){
  mkdir('./images', 0777, true);
}

这是我试过的…(来源)

 $this->load->library('curl');
 $this->load->helper('file');
 $remoteURL = 'http://fermeteogranada.com/camara.jpg';
 $img = $this->curl->simple_get($remoteURL);
 $localURL = './images/main.jpg';
 if ( ! write_file($localURL, $img)){
     $this->session->set_flashdata('err_message', 'Unable to write the file');
  }

它正在创建一个名为main.jpg但大小为0字节的文件。

我也尝试使用copy和file_get_content

if ( ! copy($localURL, $img)){
  $error = get_last_error();   
  $this->session->set_flashdata('err_message', $error['message']);
}

if ( ! file_put_content($localURL, file_get_content($img) )){
  $error = get_last_error();   
  $this->session->set_flashdata('err_message', $error['message']);
}

但是它也不能正常工作,它没有给出任何错误信息。

我将首先调试$img data验证它有一堆二进制垃圾。您可能还需要检查http状态。

这样做怎么样?

  $remoteFile = 'http://fermeteogranada.com/camara.jpg';
  $remoteResource = file_get_contents($remoteFile);
  $saveTo = './images/main.jpg';
  file_put_contents($saveTo, $remoteResource);

我在本地试过了,它工作得很好。