>  기사  >  백엔드 개발  >  php 업로드 파일

php 업로드 파일

WBOY
WBOY원래의
2016-08-08 09:24:57878검색

upLoad.php

<html>
	<head>
<!-- 		<link rel="stylesheet" href="myCss.css" type="text/css" /> -->
	</head>
	<div class="register">
		<form enctype="multipart/form-data" method="post" action="upLoadProcess.php" name="myform">
			<table>
				<tr>
					<td align="center" colspan="2"><font style="font-size:40px;font-family:华文彩云">文件上传</font></td>
				</tr>
				<tr>
					<td>请填写用户名:</td>
					<td><input type="text"  name="username"/></td>
				</tr>
				<tr>
					<td>请简单介绍该文件</td>
					<td><TEXTAREA name="fileintro" rows="10" cols="80"></TEXTAREA></td>
				</tr>
				<tr>
					<td colspan="2">请选择要上传的文件:<input type="file" name="myfile"/></td>
				</tr>
				<tr>
					<td align="center" colspan="2"><input type="submit"  value="上传文件" /></td>
				</tr>
			</table>
		</form>
	</div>
</html>
<?php 
	if(isset($_GET[&#39;flag&#39;])){
		$flag = $_GET[&#39;flag&#39;];
		if($flag == 1){
			echo "<br/<br/><font style=&#39;color:red&#39;>文件大小不能超过2M</font>";
		}
	}
?>

upLoadProcess.php

<?php
	//接收提交文件的信息
	if(isset($_POST[&#39;username&#39;])){
		$username=$_POST[&#39;username&#39;];
	}else{
		echo "上传文件失败";
		exit();
	}
	
	if(isset($_POST[&#39;fileintro&#39;])){
		$fileintro = $_POST[&#39;fileintro&#39;];
	}
	
	$file_size = $_FILES[&#39;myfile&#39;][&#39;size&#39;];
	
	if($file_size > 2*1024*1024){
		echo "<font style=&#39;color:red&#39;>文件过大,不能上传超过2M的文件</font><br/>";
		echo "<a href=&#39;upLoad.php&#39;>返回重新上传</a>";
		exit();
	}
	
	$file_type = $_FILES['myfile']['type'];
	
	if($file_type != "image/jpg" && $file_type != "image/pjpeg"){
		echo "文件类型只能是jpg的";
		exit();
	}
	
// 	echo "<pre class="brush:php;toolbar:false">";
// 	print_r($_FILES);
// 	echo "
"; if(is_uploaded_file($_FILES['myfile']['tmp_name'])){ //把文件转存到你希望的目录; $upLoaded_file = $_FILES['myfile']['tmp_name']; $fileName = $_FILES['myfile']['name']; //给每个用户动态的创建一个文件夹 $user_path = $_SERVER['DOCUMENT_ROOT']."/fileOpt/upLoadFiles/".$username; if(!file_exists($user_path)){ mkdir($user_path); } //$destPath = $user_path."/".$fileName; $destPath = $user_path."/".(time().rand(0,1000)).substr($fileName,strrpos($fileName, ".")); //如果目标文件存在,则被覆盖 if(move_uploaded_file($upLoaded_file,$destPath)){ echo $_FILES['myfile']['name']." 上传成功!"; }else{ echo "上传失败!
"; echo "返回重新上传"; } }else{ echo "上传失败!
"; echo "返回重新上传"; } ?>

위 내용은 PHP 업로드 파일에 대한 내용을 포함하여 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.