понедельник, 23 января 2012 г.

imgDownload($url)

function imgDownload($url)
{

if (file_exists($url) == false)
{
 echo 'norādītais fails neeksistē';
 return false;
}

$res = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($res,$url);
finfo_close($res);

if (($mime != 'image/jpeg') && ($mime != 'image/png') && ($mime != 'image/gif'))
{
 echo 'norādītais fails nav jpg, png, gif';
 return false;
} else if ($mime == 'image/jpeg') { $ext = 'jpg'; 
} else if ($mime == 'image/png')  { $ext = 'png'; 
} else if ($mime == 'image/gif')  { $ext = 'gif'; 
}

$file = file_get_contents($url);
if ($file == null)
{
 echo 'failu neizdevās nolasīt';
 return false;
}
/// $ext = pathinfo($url,PATHINFO_EXTENSION);
$name = md5($file);
$path = 'images/'.$ext.'/'.$name.$ext;
file_put_contents($path, $file);

if (file_exists($path) == false)
{
 echo 'failu neizdevās saglabāt';
 return false;
}

return true;
}

imgDownload($url);

Комментариев нет:

Отправить комментарий