您的位置:首页 > 编程语言 > PHP开发

PHP上传多文件,多图片程序代码

2011-09-27 16:01 696 查看
1.在同级目录下建立upload文件夹

2.将下面代码复制到upload.php中

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
$uptypes=array(
//上传文件的ContentType格式
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png',
'application/msword',//doc
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',//docx
'application/vnd.openxmlformats-officedocument.presentationml.presentation',//pptx
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',//xlsx
'text/plain'
);
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
$dir="upload/";          //上传文件路径
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$file = $_FILES['upfile']['name'];
foreach($file as $key=>$item){
if($item != ''){
if (!is_uploaded_file($_FILES['upfile']['tmp_name'][$key]))//是否存在文件
{
echo "图片不存在!";
exit;
}
if($max_file_size < $_FILES['upfile']['size'][$key])//检查文件大小
{
echo "文件太大!";
exit;
}
if(!file_exists($dir))
{
mkdir($dir);
}
$filename=$_FILES['upfile']['tmp_name'][$key];
$image_size = getimagesize($filename);
$pinfo = pathinfo($file[$key]);

$ftype = $pinfo['extension'];
$destination = $dir.time().$file[$key];
if (file_exists($destination) && $overwrite != true)
{
echo "同名文件已经存在了";
exit;
}

if(!move_uploaded_file ($filename, $destination))
{
echo "移动文件出错";
exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo['basename'];
echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$dir.$fname."</font><br>";
echo " 宽度:".$image_size[0];
echo " 长度:".$image_size[1];
echo "<br> 大小:".$_FILES['upfile']['size']." bytes";

}

echo "<br>图片预览:<br>";
echo "<img src=\"".$destination."\" width=".($image_size[0]*(1/4))." height=".($image_size[1]*(1/4));
echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">";
echo "<br>";
}
}

?>
<form method="post" enctype="multipart/form-data" action="" name="ff" id="ff" >
<input type="file" name="upfile[]" />
<input type="file" name="upfile[]" />
<label>
<input type="submit" name="submit" id="submit" value="按钮"/>
</label>
</form>
ps:上传时,需要上传两个。不然会报错,代码不是很完善,只是给出一个思路。自己可以根据此来编写代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: