感謝您的幫助。插入數據庫後重命名圖片名稱
我寫了一個腳本,它從外部URL下載圖片並寫入服務器。我使用mysql數據庫來調用圖像的目的地。爲了這個,我必須按照這個順序:
- 下載網址從照片寫進我的文件夾
- 寫destionation到我的數據庫
- 採取與mysql_insert_id($鏈接)的最後一個ID;
- 重命名圖片。 (我不知道如何與重命名做到())
這裏是我的代碼
<?php
$source_pic = $arr['thumbnail_url']; \\ comes from a API
$destination_pic = '/var/www/vhosts/domain.com/domain.com/Uploads/thumbnails/'.uniqid($tag_ponter).'.jpg';
$max_width = 460;
$max_height = 345;
$what = getimagesize($source_pic);
switch(strtolower($what['mime']))
{
case 'image/png':
$src = imagecreatefrompng($source_pic);
break;
case 'image/jpeg':
$src = imagecreatefromjpeg($source_pic);
break;
case 'image/gif':
$src = imagecreatefromgif($source_pic);
break;
default: die();
}
list($width,$height)=getimagesize($source_pic);
$x_ratio = $max_width/$width;
$y_ratio = $max_height/$height;
if(($width <= $max_width) && ($height <= $max_height)){
$tn_width = $width;
$tn_height = $height;
}elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,$destination_pic,100);
imagedestroy($src);
imagedestroy($tmp);
$thumbnail = '/Uploads/thumbnails/'.$tag_pointer.'.jpg';
//Write into Database
$values[] = "(NULL,'" . $GroupID . "','" . $UserID . "','" . $Title . "', '". $Description ."', '" . $tag_pointer . "','" . $arr['url'] . "', '". $arr['provider_name'] ."', '".$Text."', '". $arr['type'] ."', '" . $thumbnail . "', '" . $arr['html'] . "', NOW(), '0', '" . getRealIP() . "','1', '0','0')";
$insert = mysql_query("INSERT INTO contents(ContentID, CatID, UserID, Title, Description, Tag, Link, Provider, Text, Type, Thumbnail, Html, WriteTime, Comments, WriterIP, Active, TotalVotes, VoteSum)
VALUES ".implode(',', $values)."");
$ContentID = mysql_insert_id($link);
?>
我該如何重命名文件?這是正確的事情嗎?
重命名文件系統中的文件? http://php.net/manual/fr/function.rename.php – twillouer
重命名文件系統中的文件 –