Heim  >  Artikel  >  Backend-Entwicklung  >  php文件上传之单文件上传

php文件上传之单文件上传

WBOY
WBOYOriginal
2016-08-08 09:20:181044Durchsuche

为了简单一些,php文件跟form表单写在了一个文件里.

php单文件上传---->

<span> 1</span> 
<span> 2</span> 
<span> 3</span> 
<span> 4</span>     <meta charset="UTF-8">
<span> 5</span> 
<span> 6</span> 
<span> 7</span> 
8 请选择要上传的文件:

9 10 11
12 13 14151617 php 18if(!empty($_FILES)){ 19header('content-type:text/html;charset=utf-8'); 20$fileInfo=$_FILES['myfile']; 21print_r($_FILES); 22//如果上传出错则退出并打印错误信息23if($fileInfo['error']>0){ 24switch($fileInfo['error']){ 25case 1: 26$msg_error='上传文件超过了php配置文件中UPLOAD_MAX_FILESIZE选项的值'; 27break; 28case 2: 29$msg_error='超过了表单MAX_FILE_SIZE限制的大小'; 30break; 31case 3: 32$msg_error='文件部分上传'; 33break; 34case 4: 35$msg_error='没有文件上传'; 36break; 37case 6: 38$msg_error='没有找到临时目录'; 39break; 40case 7: 41case 8: 42$msg_error='系统错误'; 43break; 44 } 45exit($msg_error); 46 } 47$filename=$fileInfo['name']; 48//获取文件的扩展名49$ext=strtolower(substr($filename,strrpos($filename,'.')+1)); 50//定义可允许上传的扩展名51$allowExt=array('txt','html','png','gif','jpeg'); 52//检测上传文件的类型53if(!in_array($ext,$allowExt)){ 54exit('上传文件类型错误'); 55 } 565758//检测文件的大小59$maxSize=2097152; 60if($fileInfo['size']>$maxSize){ 61exit('上传文件过大'); 62 } 6364//检测是否为HTTP POST方式上传上来的65if(!is_uploaded_file($fileInfo['tmp_name'])){ 66exit('文件不是通过HTTP POST方式提交上来的'); 67 } 6869//确保文件名字唯一,防止同名文件被覆盖70$uniqName=md5(uniqid(microtime(true),true)).'.'.$ext; 7172//定义保存在哪个文件夹下,如果没有该文件夹则创建73$path='uploads'; 74if(!file_exists($path)){ 75mkdir($path,0777,true); 76chmod($path,0777); 77 } 78$destination=$path.'/'.$uniqName; 7980//移动文件至要保存的目录81if(! @move_uploaded_file($fileInfo['tmp_name'],$destination)){ 82exit('文件上传失败'); 83 } 8485echo '上传成功'; 8687 } 88 ?>

以上就介绍了php文件上传之单文件上传,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn