Home > php教程 > PHP源码 > body text

PHP 文件上传

PHP中文网
Release: 2016-05-25 17:14:19
Original
1002 people have browsed it

php代码

<html>
<head>
<title>图片上传</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="upform">
上传文件:<input type="file" name="upfile" value="">
<input type="submit" value="上传"><br>
<?php
//能够上传的类型
$uptypes=array(&#39;image/jpg&#39;,&#39;image/jpeg&#39;,&#39;image/png&#39;,&#39;image/pjpeg&#39;,&#39;image/gif&#39;,
&#39;image/bmp&#39;,&#39;image/x-png&#39;);
?>
允许上传的文件类型为:<?=implode(&#39;,&#39;,$uptypes)?>
</form>
</body>
</html>
<?php
$max_file_szie=2*pow(2,20);  //上传的文件小于2MB
$destination_folder=&#39;uploadimg/&#39;; //上传文件保存路径
if($_SERVER[&#39;REQUEST_METHOD&#39;]==&#39;POST&#39;){  
		if(!is_uploaded_file($_FILES[&#39;upfile&#39;][&#39;tmp_name&#39;])){
				echo &#39;图片不存在!&#39;;
				exit;
			}
		if($max_file_szie<$_FILES[&#39;upfile&#39;][&#39;size&#39;]){
				echo &#39;文件太大了!&#39;;
				exit;
			}
		if(!in_array($_FILES[&#39;upfile&#39;][&#39;type&#39;],$uptypes)){
				echo &#39;文件类型不符合!&#39;.$_FILES[&#39;upfile&#39;][&#39;type&#39;];
				exit;
			}
		if(!file_exists($destination_folder)){
				mkdir($destination_folder);
            }
		$filename=$_FILES[&#39;upfile&#39;][&#39;tmp_name&#39;];
		$image_size=getimagesize($filename);
		$pinfo=pathinfo($_FILES[&#39;upfile&#39;][&#39;name&#39;]); //文件路径信息
		$ftype=$pinfo[&#39;extension&#39;]; //旧文件后缀名
		$destination = $destination_folder.time().".".$ftype; //新文件名称
		if(file_exists($destination)&&$voerwrie !=true){
				echo &#39;同名文件已经存在了!&#39;;
				exit;
			}
		//把上传的文件从临时文件夹移动到指定目录
		if(!move_uploaded_file($filename,$destination)){ 
				echo &#39;移动文件出错了!&#39;;
				exit;
			}
		$pinfo=pathinfo($destination);
		$fname=$pinfo[basename];
		echo "<font color=&#39;red&#39;>已经成功上传</font><br>文件名:
              <font color=&#39;blue&#39;>".$destination_folder.$fname."</font><br>";
		echo &#39;宽度:&#39;.$image_size[0];
		echo &#39;高度:&#39;.$image_size[1];
		echo &#39;<br>大小:&#39;.$_FILES[&#39;upfile&#39;][&#39;size&#39;]."bytes";
	}
?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!