今天主要学习了PHP上传文件并将文件地址和文件名写入数据库,现在简单的写了个,能用,简单的限制条件都实现了,个人觉得还是可以的,现在把源代码贴出来,高手多多指教哦!
[php]
<?php /****************************************************************************** 参数说明: $max_file_size : 上传文件大小限制, 单位BYTE $destination_folder : 上传文件路径 ******************************************************************************/ include "conn.php"; //上传文件类型列表 $uptypes=array( 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/pdf', 'application/msword', 'image/x-png' ); $max_file_size=2000000; //上传文件大小限制, 单位BYTE $destination_folder="up1/"; //上传文件路径 ?>
<html>
<head>
<title>文件上传程序</title>文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%20type%3D%22text%2Fcss%22%3E%0A%3C!--%20body%20%7B%20font-size%3A%209pt%3B%20%7D%20input%20%7B%20background-color%3A%20%2366CCFF%3B%20border%3A%201px%20inset%20%23CCCCCC%3B%20%7D%20--%3E%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<style>" title="<style>" />文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
</head>文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
<body>文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
<form enctype="multipart/form-data" method="post" name="upform">
上传文件:
<input name="upfile" type="file">
<input type="submit" value="上传">
允许上传的文件类型为:<?php echo implode(',',$uptypes)?>
</form>文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"]['tmp_name']))
//是否存在文件
{
echo "您还没有选择文件!";
exit;
}文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "您选择的文件太大了!";
exit;
}文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "文件类型不符!".$file["type"];
exit;
}文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
if(!file_exists($destination_folder))
{
mkdir($destination_folder);
}文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo['extension'];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) && $overwrite != true)
{
echo "同名文件已经存在了";
exit;
}文章源自陈学虎-https://chenxuehu.com/article/2012/09/886.html
if(!move_uploaded_file ($filename, $destination))
{
echo "移动文件出错";
exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo['basename'];
echo " <font color=red>已经成功上传</font>
完整地址: <font color=blue>http://localhost/new/".$destination_folder.$fname."</font>
";
echo "
大小:".$file["size"]." bytes";
echo '
';
//将数据插入到数据库中
$dizhi = "http://localhost/new/"."$destination_folder"."$fname";
$name = $file['name'];
$sql = "insert into `excel`(`id`,`dizhi`,`name`) values ('NULL','$dizhi','$name')";
mysql_query($sql);
echo "数据插入成功";
}
?>
</body>
[/php]
这个是在本机测试的,现在还在学习阶段,等学习好了,就要派上用场了,现在很想把这个好好学学啊,但是感觉难啊!
评论