1. 程式人生 > >遠程下載圖片

遠程下載圖片

req 遠程 lose name strtol web pat amp oot

function get_url_image($url)
{
$ext = explode(‘.‘, $url);
$ext = strtolower(end($ext));
if($ext != "gif" && $ext != "jpg" && $ext != "png" && $ext != "bmp" && $ext != "jpeg")
{
return $url;
}

$name = date(‘Ymd‘);
for ($i = 0; $i < 6; $i++)
{
$name .= chr(mt_rand(97, 122));
}
$name .= ‘.‘ . $ext;
$target = ROOT_PATH . DATA_DIR . ‘/afficheimg/‘ . $name;

$tmp_file = DATA_DIR . ‘/afficheimg/‘ . $name;
$filename = ROOT_PATH . $tmp_file;

$img = file_get_contents($url);

$fp = @fopen($filename, "a");
fwrite($fp, $img);
fclose($fp);

return $tmp_file;
}
————————————————
// 主圖上傳
if ($_FILES[‘topic_img‘][‘name‘] && $_FILES[‘topic_img‘][‘size‘] > 0)
{
/* 檢查文件合法性 */
if(!get_file_suffix($_FILES[‘topic_img‘][‘name‘], $allow_suffix))
{
sys_msg($_LANG[‘invalid_type‘]);
}

/* 處理 */
$name = date(‘Ymd‘);
for ($i = 0; $i < 6; $i++)
{
$name .= chr(mt_rand(97, 122));
}
$name .= ‘.‘ . end(explode(‘.‘, $_FILES[‘topic_img‘][‘name‘]));
$target = ROOT_PATH . DATA_DIR . ‘/afficheimg/‘ . $name;

if (move_upload_file($_FILES[‘topic_img‘][‘tmp_name‘], $target))
{
$topic_img = DATA_DIR . ‘/afficheimg/‘ . $name;
}
}
else if (!empty($_REQUEST[‘url‘]))
{
/* 來自互聯網圖片 不可以是服務器地址 */
if(strstr($_REQUEST[‘url‘], ‘http‘) && !strstr($_REQUEST[‘url‘], $_SERVER[‘SERVER_NAME‘]))
{
/* 取互聯網圖片至本地 */
$topic_img = get_url_image($_REQUEST[‘url‘]);
}
else{
sys_msg($_LANG[‘web_url_no‘]);
}
}

遠程下載圖片