赞
踩
- <?php
- //case 22 用户头像上传
- $info = array('id'=>234,'name'=>'王五');
-
- ?>
-
- <html>
- <head>
- <title>用户头像上传</title>
- </head>
- <body>
- <form action="" method="post" enctype="multipart/form-data">
- <h2>编辑用户头像</h2>
- <p>用户姓名:<?php echo $info['name'];?></p>
- <p>现有头像:</p>
- <img src="<?php echo './img/'.$info['id'].'.jpg?rand='.rand();?>" onerror="this.src='./img/default.jpg'" border="1"/>
- <br>
- 上传头像:<input name="pic" type="file"><br>
- <input type="submit" name="up_load" value="保存头像">
- </form>
- </body>
- </html>
-
- <?php
- if(isset($_FILES['pic']))
- {
- $pic_info = $_FILES['pic'];
- if($pic_info['error'] > 0)
- {
- $error_msg = '上传错误:';
- switch ($pic_info['error']) {
- case 1:
- $error_msg .= '文件大小超过了php配置文件中upload_max_filesize选项的限制值';
- break;
- case 2:
- $error_msg .= '文件大小超过了表单中max_file选项的值';
- break;
- case 3:
- $error_msg .= '文件只有部分被上传';
- break;
- case 4:
- $error_msg .= '没有文件被上传';
- break;
- case 6:
- $error_msg .= '找不到临时文件夹';
- break;
- case 7:
- $error_msg .= '文件写入失败!';
- break;
- default:
- $error_msg .= '未知错误';
- break;
- }
- die($error_msg);
- }
- $type = substr(strrchr($pic_info['name'],'.'),1);
- if ($type !== 'jpg')
- {
- die('图像类型不符合要求,允许的类型为:jpg');
- }
-
- $new_file = $info['id'].'.jpg';
- $file_name = './img/'.$new_file;
- if(!move_uploaded_file($pic_info['tmp_name'],$file_name))
- {
- echo "头像上传失败";
- }
- }
- ?>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。