1 回答
TA贡献1799条经验 获得超8个赞
move_uploaded_file() 只是移动上传的文件,你可以使用它一次,但你可以使用 copy() 函数复制文件多次,就像这个例子一样
move_uploaded_file($uploaded, $destination);
copy($destination, $destination2);
但是您的代码存在重大安全问题,因为您允许上传每种类型的文件,这可能使任何人都可以破解您的网站。
我建议你:
添加accept='image/*'到input type='file'您的 html 中。
通过以下代码检查文件扩展名和 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"))){}
然后从临时文件创建图像:
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)创建图像并将其保存在路径中,您可以随意使用这两个功能,并且imagedestroy($image)完成后不要忘记使用。
如果您还需要什么,请告诉我。
编辑:添加新代码
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 关注
- 86 浏览
添加回答
举报