Home > Backend Development > PHP Tutorial > PHP example four file upload

PHP example four file upload

WBOY
Release: 2016-08-08 09:31:23
Original
851 people have browsed it

This is the form code:

<form method = "post" action = "upload.php" enctype="multipart/form-data">;
 <input type = "hidden" name = "MAX_FILE_SIZE" value ="10000000">
   需求上传:<input type = "file" name = "myfile" >;
<p> <input type = "submit" name = "submit" value = "确定上传"></p>;
</form>;
Copy after login

This is the upload.php code:

<?php
 $allowtype = array("gif","png","jpg","doc","txt");     //上传图片的格式
 $size = 10000000;                         //允许最大上传是10M
 $path = "./uploads";                     //上传后的路径
 
//判断是否成功的上传了服务器,0表示上传成功
 if($_FILES[&#39;myfile&#39;][&#39;error&#39;] > 0)
 {
   echo '上传错误:';
   switch($_FILES['myfile']['error']){
     case 1: die('上传大小超过了限定值:upload_max_filesize');
     case 2: die('上传大小超过了表单中的约定值:MAX_FILE_SIZE');
	 case 3: die('文件只有部分上传!');
	 case 4: die('没有上传任何文件!');
	 default:die('未知错误');
   }
 }
  $linshi =explode(".",$_FILES['myfile']['name']);
//判断上传的文件是否符合允许的格式
 $hz = array_pop($linshi);//数组获取文件的名称包括扩展名
 //方法是判断后缀
 if(!in_array($hz,$allowtype))
  {
    die("这个后缀<b>{$hz}</b>不是允许的文件类型");
  }

//判断大小是否符合
  if($_FILES['myfile']['size']>$size)
  {
   die('超过了允许的<b>{$size}</b>字节大小');
    
  }
  //$filename = date("YmdHis").rand(100,999).".".$hz;

 if(is_uploaded_file($_FILES['myfile']['tmp_name']))
 {
   if(!move_uploaded_file($_FILES['myfile']['tmp_name'],$path.'/'.$_FILES['myfile']['name']))
	 {
           die('不能将文件移至指定目录!');   
     }
	 

  
   
 }else
	 {
	       die('不是指定文件!');
	 }

	 
   echo"<center>";
	 echo "您好!</br>";
echo "您的文件:{$_FILES['myfile']['name']}已上传成功!大小为{$_FILES['myfile']['size']}字节!";
echo"我们会及时处理并联系您,请您耐心等待。";
echo "</br>";
echo "<a href = &#39;index.php&#39;>返回首页</a>";

echo "</center>";



?>
Copy after login

The above introduces the uploading of files in PHP example four, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template