Blogger Information
Blog 34
fans 0
comment 0
visits 28437
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
419文件上传
1A7498的博客
Original
1015 people have browsed it
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data">
	<!-- 用隐藏域设置允许上传的文件大小,仅考参考 -->
	<input type="hidden" name="MAX_FILE_SIZE" value="542488">
	<fieldset>
		<legend align="center">
			文件上传
		</legend>
		<p>
			<strong>选择文件:</strong>
			<input type="file" name="upload">
		</p>
	</fieldset>
	<p align="center">
		<button type="submit" name="submit" >
		上传
		</button>
	</p>
</form>
<script src="../js/jquery.js"></script>
<!--<script>
	$('button').click(function(){
		var data = formFile;
		$.ajax({
			type:"post",
			url:"419.php",
			async:true,
			dataType:'json',
			data:new FormData(),
			success:function(msg,status,xhr){
				alert('ajax完成回调');
			}
		});
	})
</script>-->
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	if (isset($_FILES['upload'])){
		$allow = ['image/jpg','image/jpeg','image/png',];
		if (in_array($_FILES['upload']['type'], $allow)){
			if (move_uploaded_file($_FILES['upload']['tmp_name'], "upload/{$_FILES['upload']['name']}")){
				echo '<script>$("form").after("<p>上传成功</p>")</script>';
			}
		} else {
					//提示格式不对
					echo '<script>$("form").after("<p>仅允许上传jpg和png格式的图片</p>")</script>';
				}
		}
	
	//对上传错误进行处理
		if ($_FILES['upload']['error'] > 0 ) {
			echo '<p>错误原因是:<strong>';

			switch ($_FILES['upload']['error']) {
				case 1:
					echo '文件超过了php.ini配置中设置的大小';
					break;
				case 2:
					echo '文件超过了表单中常量设置的大小';
					break;
				case 3:
					echo '仅有部分文件被上传';
					break;
				case 4:
					echo '没有文件被上传';
					break;
				case 6:
					echo '没有可用的临时文件夹';
					break;
				case 7:
					echo '磁盘已满,写入失败';
					break;
				case 8:
					echo '上传意外中止';
					break;
				
				default:
					echo '系统未知错误';
					break;
			}

			echo '</strong></p>';
			//保险起见,最好把创建的临时文件删除,当然系统也会在结束会话时自动清空
			if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) {
				unlink($_FILES['upload']['tmp_name']);
			}
		}
	} else {
		echo '1';
	}




//1.分解文件名

//系统常量__FILE__
echo __FILE__, '<br>';

//1.获取文件名
echo '文件名: ' . basename(__FILE__) . '<br>';

//2.获取目录名
echo '目录名: ' . dirname(__FILE__) . '<br>';
//php5.3+推荐使用常量__DIR__代替dirname()
echo __DIR__, '<br>';

//3.pathinfo():将目录名,文件名,扩展或解析到一个数组中
$pathinfo = pathinfo(__FILE__);
echo '目录名:', $pathinfo['dirname'], '<br>';
echo '文件名:', $pathinfo['basename'], '<br>';
echo '扩展名:', $pathinfo['extension'], '<br>';

$path = dirname(__FILE__) . DIRECTORY_SEPARATOR . basename(__FILE__);
echo $path, '<br>';

var_dump($path == __FILE__);
//此晚, $path与__FILE__完全相同

//unlink('log.php') or die('找不到文件');
copy('maxim.txt', '../419/maxim01.txt') or die('复制失败');
rename('maxim01.txt', 'maxim02.txt') or die('移动失败');
rename('maxim02.txt', __DIR__ . '/../419/upload/maxim02.txt') or die('移动失败');
mkdir('admin');
rmdir('admin');
?>
Correction status:unqualified

Teacher's comments:作业重新写一下,这什么啊?
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
  • 2018-03-16 11:39:01