-2

OK,... this is in reference to: Copy Image from Remote Server Over HTTP

Here is my code:

for ($i = 0; $i < count($json_post['Category']); ++$i )
{
    echo $json_post['Category'][$i]['CategoryID'] . '<br />';
    echo $json_post['Category'][$i]['Name'] . '<br />';
    echo $json_post['Category'][$i]['Image'] . '<br />';
    $image_URL = "https://$_SSActiveWear_BaseURL/" . $json_post['Category'][$i]['Image'];
    echo $image_URL . "<br /><br />";
    copy("https://$_SSActiveWear_BaseURL/$image_URL", $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));
    die;
}

I have tried cURL with the same results.

What is happening is that the files are being created, but with all the same file length of 58k and when I attempt to open one to view it, it is unable to be opened. In fact its the HTML content of the index page from the server I am attempting to save the image from.

Edit 1 If I hard code the image to be saved instead of using the variables, it saves the correct image.

Community
  • 1
  • 1
John Schultz
  • 672
  • 1
  • 10
  • 29

2 Answers2

1

Figured this one out.

did the following change:

copy($image_URL, $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));

I do not understand why when using a single variable, as in this case, it works and not when I use a compounded statement.

John Schultz
  • 672
  • 1
  • 10
  • 29
0

It's because your URL get interpreted wrong. Always use {} around variable in string:

copy("https://$_SSActiveWear_BaseURL/$image_URL", $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));

is converted to copy("https://SSActiveWear_BaseUrl/_URL", ...). As you can see PHP does not find variables $_ and $image and applies null for them.

Correct syntax:

copy("https://{$_SSActiveWear_BaseURL}/{$image_URL}", "{$_SERVER['DOCUMENT_ROOT']}/tmp/".basename($image_URL));
Justinas
  • 41,402
  • 5
  • 66
  • 96