I've an image from this link. I want to copy it to my server. This is what I've tried:
$url = 'http://tex.z-dn.net/?f=4%5E%7B13%7D%2A2%5E%7B-10%7D%3A16%5E%7B3%7D ';
function GetImageByURL($url, $path = null) {
$opts = array(CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10);
$ch = curl_init();
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
if ( (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200
&& (strpos(curl_getinfo($ch, CURLINFO_CONTENT_TYPE), "image") !== false)))
{
$name = substr($url, strrpos($url, '/')+1);
if ($path) {
if (is_writeable($path))
file_put_contents(rtrim($path, '/') . '/' . $name, $result);
echo $result;
} else {
echo "error : {$path}";
}
return $name;
}
curl_close($ch);
return false;
}
The above doesn't work, though.
This also doesn't work:
$input = $url;
$output = 'imageimage.jpg';
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/upload/1/',$output, file_get_contents($input));