PHP实现简单的图片上传(可限制类型)_PHP教程
程序员文章站
2022-03-25 20:01:16
...
Copy to Clipboard引用的内容:[www.bkjia.com]
$uptypes = array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$file = $_FILES["upfile"];
$fname = $_FILES["upfile"]["name"];
$fname_array = explode('.',$fname);
$extend = $fname_array[count($fname_array)-1];
$MAX_FILE_SIZE = 512000;
//文件当前位置创建picture文件夹,若要在上一层目录创建则为"../picture/";
$dest_folder = "picture/";
if($extend!=""){
if(!in_array($file["type"],$uptypes)){
echo "只能上传图片文件!";
exit;
}
if($file["size"]>$MAX_FILE_SIZE){
echo "图片大小不能超过512KB!";
exit;
}
if(!file_exists($dest_folder)){
mkdir($dest_folder);
}
$randval = date('Ymd').rand();
$uploadfile = $dest_folder.$randval.'.'.$extend;
echo 'uploadfile: '.$uploadfile ;
if(move_uploaded_file($_FILES["upfile"]["tmp_name"],$uploadfile)){
echo "图片上传成功!";
}else{
echo "图片上传失败!";
}
}
}
?>
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
代码:
Copy to Clipboard引用的内容:[www.bkjia.com]代码:
Copy to Clipboard引用的内容:[www.bkjia.com] if($_POST['Submit']=='上传'){$file = $_FILES["upfile"];
$fname = $_FILES["upfile"]["name"];
$fname_array = explode('.',$fname);
$extend = $fname_array[count($fname_array)-1];
$MAX_FILE_SIZE = 512000;
//文件当前位置创建picture文件夹,若要在上一层目录创建则为"../picture/";
$dest_folder = "picture/";
if($extend!=""){
if(!in_array($file["type"],$uptypes)){
echo "只能上传图片文件!";
exit;
}
if($file["size"]>$MAX_FILE_SIZE){
echo "图片大小不能超过512KB!";
exit;
}
if(!file_exists($dest_folder)){
mkdir($dest_folder);
}
$randval = date('Ymd').rand();
$uploadfile = $dest_folder.$randval.'.'.$extend;
echo 'uploadfile: '.$uploadfile ;
if(move_uploaded_file($_FILES["upfile"]["tmp_name"],$uploadfile)){
echo "图片上传成功!";
}else{
echo "图片上传失败!";
}
}
}
?>