草鸟写的上传文件代码[仅适合初学者],代码简洁易懂
时间:2009-05-03
来源:互联网
[php]
<?php
/****************************************************
参数说明:
$max_file_size :设置上传文件大小
$destination_folder :上传文件保存的路径
$imageview :是否生成缩略图(1为生成,其他为不生成)
$imageviewsize :缩略图比例(默认为1/2,也就是50%的大小)
作者:Longxi
邮箱:[email protected]
*****************************************************/
//上传文件类型
$FileTypes=array(
'image/jpg',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/gif'
);
$max_file_size=100000; //限制上传文件大小
$destination_folder='upfile/'; //上传文件保存路径
$imageview=1; //是否生成缩略图(1为生成,其他未不生成);
$imageviewsize=1/2; //缩略图比例;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片上传 Powered By Longxi</title>
<style type="text/css">
body{font-size:14px;}
input{border:1px #d8d8d8 solid;}
</style>
</head>
<body>
<form enctype="multipart/form-data" method="POST" action="" name="upform">
<input type="file" name="file" /><br /><br />
<input type="submit" name="submit" value="开始上传" />
</form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!is_uploaded_file($_FILES['file'][tmp_name])){
echo '文件不存在';
exit;
}
$upfile=$_FILES['file'];
if($max_file_size < $upfile['size']){
echo '文件过大';
exit;
}
if(!in_array($upfile[type],$FileTypes)){
echo '文件类型不符合';
exit;
}
if(!file_exists($destination_folder)){
mkdir($destination_folder);
}
//定义上传文件变量
$filename=$upfile['tmp_name'];
$file_size=getimagesize($filename);
$pinfo=pathinfo($upfile['name']);
// print_r($pinfo);
$file_type=$pinfo['extension'];
$destination=$destination_folder.time().".".$file_type;
// echo $destination;
if(file_exists($destination)){
echo "<br />文件已经存在,没有上传成功!";
exit;
}
if(!move_uploaded_file($filename,$destination)){
echo '文件上传出错';
exit;
}
$pinof=pathinfo($destination);
//判断是否是效的图片
if(!(@getimagesize($destination))){
echo '禁止非法操作';
//删除图片
unlink($destination);
exit;
}
$image_size=getimagesize($destination);
// print_r($image_size);
// print_r($pinfo);
echo '文件已经上传成功,文件名为'.$pinof[basename]."<br /><a href='".$destination_folder.$pinof[basename]."' target='_blank'>打开大图片</a>";
echo '<br />文件大小'.filesize($destination).'bytes';
echo '<br />原图宽度'.$image_size[0];
echo '<br />原图高度'.$image_size[1].'<br />';
//生成缩略图
if($imageview==1){
echo '图片缩略图<br />';
echo "<img src='".$destination."' width=".(floor($image_size[0]*$imageviewsize))." height=".(floor($image_size[1]*$imageviewsize))." border=1 />";
}
}
?>
</body>
</html>
[/php]
<?php
/****************************************************
参数说明:
$max_file_size :设置上传文件大小
$destination_folder :上传文件保存的路径
$imageview :是否生成缩略图(1为生成,其他为不生成)
$imageviewsize :缩略图比例(默认为1/2,也就是50%的大小)
作者:Longxi
邮箱:[email protected]
*****************************************************/
//上传文件类型
$FileTypes=array(
'image/jpg',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/gif'
);
$max_file_size=100000; //限制上传文件大小
$destination_folder='upfile/'; //上传文件保存路径
$imageview=1; //是否生成缩略图(1为生成,其他未不生成);
$imageviewsize=1/2; //缩略图比例;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片上传 Powered By Longxi</title>
<style type="text/css">
body{font-size:14px;}
input{border:1px #d8d8d8 solid;}
</style>
</head>
<body>
<form enctype="multipart/form-data" method="POST" action="" name="upform">
<input type="file" name="file" /><br /><br />
<input type="submit" name="submit" value="开始上传" />
</form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!is_uploaded_file($_FILES['file'][tmp_name])){
echo '文件不存在';
exit;
}
$upfile=$_FILES['file'];
if($max_file_size < $upfile['size']){
echo '文件过大';
exit;
}
if(!in_array($upfile[type],$FileTypes)){
echo '文件类型不符合';
exit;
}
if(!file_exists($destination_folder)){
mkdir($destination_folder);
}
//定义上传文件变量
$filename=$upfile['tmp_name'];
$file_size=getimagesize($filename);
$pinfo=pathinfo($upfile['name']);
// print_r($pinfo);
$file_type=$pinfo['extension'];
$destination=$destination_folder.time().".".$file_type;
// echo $destination;
if(file_exists($destination)){
echo "<br />文件已经存在,没有上传成功!";
exit;
}
if(!move_uploaded_file($filename,$destination)){
echo '文件上传出错';
exit;
}
$pinof=pathinfo($destination);
//判断是否是效的图片
if(!(@getimagesize($destination))){
echo '禁止非法操作';
//删除图片
unlink($destination);
exit;
}
$image_size=getimagesize($destination);
// print_r($image_size);
// print_r($pinfo);
echo '文件已经上传成功,文件名为'.$pinof[basename]."<br /><a href='".$destination_folder.$pinof[basename]."' target='_blank'>打开大图片</a>";
echo '<br />文件大小'.filesize($destination).'bytes';
echo '<br />原图宽度'.$image_size[0];
echo '<br />原图高度'.$image_size[1].'<br />';
//生成缩略图
if($imageview==1){
echo '图片缩略图<br />';
echo "<img src='".$destination."' width=".(floor($image_size[0]*$imageviewsize))." height=".(floor($image_size[1]*$imageviewsize))." border=1 />";
}
}
?>
</body>
</html>
[/php]
作者: cnlongxi 发布时间: 2009-05-03
8错!
作者: wendy 发布时间: 2009-05-03
不错!
作者: yzf3369 发布时间: 2009-05-06
好东西,代码清楚,有条理,流程。
作者: volley 发布时间: 2009-05-07
很好..
作者: cnlongxi 发布时间: 2009-05-12
看看去
作者: cyr11 发布时间: 2009-05-12
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28