0

I have simple CI (CodeIgniter) code. what I want to do is: when I enter the pictures path to textarea, php downloaded each picture to my server and gave them filenames, but I have these errors:

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

Here is my CI code:

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

What it does is simply downloads last file and for first and second files gives me that error

and please don't give me other advice like using cURL of fopen. I think that it is correct way because php downloads LAST FILE successfully. Please help me to solve my problem

Irakli
  • 1,151
  • 5
  • 30
  • 55

3 Answers3

3

From the manual:

A URL can be used as a filename with this function if the fopen wrappers have been enabled.

You will either need to set allow_url_fopen to true or use cURL instead.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • I would like to know WHY MY CODE DOESN'T WORK? What mistake I have done – Irakli Jan 13 '12 at 21:23
  • You need to check your php.ini for the `allow_url_fopen` setting. If this isn't set properly, `file_get_contents` will fail. – webbiedave Jan 13 '12 at 21:26
  • @user939421 as webbiedave mentioned it might be disabled on your server side, check your phpinfo or use cURL extention – Nazariy Jan 13 '12 at 21:26
  • please look at my modified code, that can help you to help me, plzz – Irakli Jan 14 '12 at 11:28
  • Do as webbiedave has stated. Make a PHP script with only `=phpinfo();?>` run that, and check if `allow_url_fopen` is set. – Alasdair Jan 14 '12 at 11:41
  • it is not set on, and I can't set it on too, it is disabled by server whis is not going to enable it. But most important is that php downloads third picture completly but has problems with first two pictures – Irakli Jan 14 '12 at 12:20
1

I don't know Why the problem happened but here is the code that fixed it, thanks to "NikiC" who found out that witespace " " was the problem, I fixed it.

This Code doesn't work

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

This is working code

$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
  • 1,151
  • 5
  • 30
  • 55
0

Wrap your URL in quotes; make it a string

atxdba
  • 5,158
  • 5
  • 24
  • 30