您好,欢迎访问一九零五行业门户网

php文件上传之单文件上传

为了简单一些,php文件跟form表单写在了一个文件里.
php单文件上传---->
1 2 3 4 5 6 7
8 请选择要上传的文件:
9 10 11 12 13 14151617 php18if(!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教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息