1 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
move_uploaded_file() 只是移動(dòng)上傳的文件,你可以使用它一次,但你可以使用 copy() 函數(shù)復(fù)制文件多次,就像這個(gè)例子一樣
move_uploaded_file($uploaded, $destination);
copy($destination, $destination2);
但是您的代碼存在重大安全問題,因?yàn)槟试S上傳每種類型的文件,這可能使任何人都可以破解您的網(wǎng)站。
我建議你:
添加accept='image/*'到input type='file'您的 html 中。
通過以下代碼檢查文件擴(kuò)展名和 MIME 類型:
$ext=explode(".",$_FILES['image']['name'][$i]);
$ext=strtolower($ext[1]);
if ((($_FILES['image']['type'][$i] == "image/gif")
|| ($_FILES['image']['type'][$i] == "image/jpeg")
|| ($_FILES['image']['type'][$i] == "image/jpg")
|| ($_FILES['image']['type'][$i] == "image/pjpeg")
|| ($_FILES['image']['type'][$i] == "image/x-png")
|| ($_FILES['image']['type'][$i] == "image/png"))
&& ($_FILES['image']['size'][$i] < 5000000)
&& in_array($ext,array("gif","jpeg","jpg","png"))){}
然后從臨時(shí)文件創(chuàng)建圖像:
switch ($ext){ case 'jpg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name'][$i]);break; case 'jpeg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name'][$i]);break; case 'png':$image=imagecreatefrompng($_FILES['image']['tmp_name'][$i]);break; case 'gif':$image=imagecreatefromgif($_FILES['image']['tmp_name'][$i]);break; }
使用imagejpeg($image,$path,75)或imagepng($image,$path,8)創(chuàng)建圖像并將其保存在路徑中,您可以隨意使用這兩個(gè)功能,并且imagedestroy($image)完成后不要忘記使用。
如果您還需要什么,請(qǐng)告訴我。
編輯:添加新代碼
if($_FILES['image']['name']!=''){
$ext=explode(".",$_FILES['image']['name']);
$ext=strtolower($ext[1]);
if ((($_FILES['image']['type'] == "image/gif")
|| ($_FILES['image']['type'] == "image/jpeg")
|| ($_FILES['image']['type'] == "image/jpg")
|| ($_FILES['image']['type'] == "image/pjpeg")
|| ($_FILES['image']['type'] == "image/x-png")
|| ($_FILES['image']['type'] == "image/png"))
&& ($_FILES['image']['size'] < 12000000)
&& in_array($ext,array("gif","jpeg","jpg","png"))){
$is_jpg=true;$ex='jpg';
switch ($ext){
case 'jpg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name']);break;
case 'jpeg':$image=imagecreatefromjpeg($_FILES['image']['tmp_name']);break;
case 'png':$image=imagecreatefrompng($_FILES['image']['tmp_name'][$i]);$ex='png';$is_jpg=false;break;
case 'gif':$image=imagecreatefromgif($_FILES['image']['tmp_name']);$ex='gif';$is_jpg=false;break;
default:$image=imagecreatefromjpeg($_FILES['image']['tmp_name']);break;
}
//set random name for img to avoid duplicate name
$img_name='upload_'.mb_substr(md5($_FILES['image']['name'].rand(0,50)),0,rand(5,10)).'.'.$ex;
$width=imagesx($image);$height=imagesy($image);
//if image jpg
if($is_jpg){
//change to your upload path
isset($_POST['combat']){imagejpeg($image,'combat/images/'.$img_name,80);}
isset($_POST['personnage']){imagejpeg($image,'search/personnages/'.$img_name,80);}
imagedestroy($image);
}
// if image png or gif to save alpha . if gif will not be animated
else{
$bg = imagecreatetruecolor($width, $height);
imagesavealpha($bg, TRUE);
imagefill($bg, 0, 0, imagecolorallocatealpha($bg, 0, 0, 0,127));
imagecopy($bg, $image, 0, 0, 0, 0,$width, $height);
imagedestroy($image);
isset($_POST['combat']){imagepng($bg,'combat/images/'.$img_name,8);}
isset($_POST['personnage']){imagepng($bg,'search/personnages/'.$img_name,8);}
imagedestroy($bg);
}
}
}
- 1 回答
- 0 關(guān)注
- 125 瀏覽
添加回答
舉報(bào)