下面的代码演示了php中如何获取用户上传的文件,并限制文件类型的一般图片文件,最后保存到服务器
HTML源码:
Upload FormUpload a file:
php上传并判断文件类型源码
$aErrors = "";
if ( !empty( $thefile_name ) ) // no file selected
{
$$thefile_type=$_FILES["file"]["type"];
if ( ( $thefile_type == "image/gif" ) ||
( $thefile_type == "image/pjpeg" ) ||
( $thefile_type == "image/jpeg" ) ){
if ( $thefile_size < ( 1024 * 100 ) ){
$aCurBasePath = dirname( $PATH_TRANSLATED );
$aNewName = $aCurBasePath . $thefile_name;
copy( $thefile, $aNewName );
} else {
$aErrors .= "超过最大上传文件限制";
}
} else {
$aErrors .= "文件不是图片";
}
} else{
$aErrors .= "没有选中任何文件上传";
}
?>