Home >php教程 >PHP源码 >简单上传文件源码

简单上传文件源码

PHP中文网
PHP中文网Original
2016-05-23 17:09:252058browse

php代码

<?php
// 注册表单的姓名
$name="";
$nameErr="";
if ($_SERVER["REQUEST_METHOD"]=="POST") {
	if (empty($_POST[&#39;name&#39;])) {
	}else{
		$name=$_POST[&#39;name&#39;];
		if (!preg_match("/^[a-zA-Z]*$/", $name)) {
			$nameErr="只允许字母和空格";
		}else{
			echo &#39;姓名&#39;.$name;
		}
	}

	// 文件上传
		if ((($_FILES["file"]["type"] == "image/gif")
		|| ($_FILES["file"]["type"] == "image/jpeg")
		|| ($_FILES["file"]["type"] == "image/pjpeg"))
		&& ($_FILES["file"]["size"] < 2000000))
    	{

			if ($_FILES["file"]["error"]>0) {
				echo "错误:".$_FILES["file"]["error"]."
";

			}else{
				echo "upload:".$_FILES["file"]["name"]."
";
				echo "type:".$_FILES["file"]["type"]."
";
				echo "size:".$_FILES["file"]["size"]."
";
				echo "stored in:".$_FILES["file"]["tmp_name"];
			}	
		}else{
			if (file_exists("weiwei/".$_FILES["file"]["name"])) {
				echo $_FILES["file"]["name"]."上传成功.";
			}else{
				 move_uploaded_file($_FILES["file"]["tmp_name"],
		      "weiwei/" . $_FILES["file"]["name"]);
		      echo "Stored in: " . "weiwei/" . $_FILES["file"]["name"];
			}

	echo "上传成功";

}

}
	
?>

Statement:
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