PHP文件上传简单实例
<?php header("content-type:text/html;charset=utf-8"); $path = getcwd()."/upload/"; if($_POST['submit']) { $fname = $_FILES['ufile']['name']; $tmp_name = $_FILES['ufile']['tmp_name']; //允许上传的文件后缀名: $pathinfo = pathinfo($fname); $fext = $pathinfo['extension']; $allowf = array("gif","GIF","png","PNG","jpeg","JPEG","jpg","JPG"); if(!in_array($fext,$allowf)) { echo "不允许的文件类型,<a href='test.php'>请重新上传</a>"; exit; } /* 另一种检验允许通过的文件类型方法: $type = $_FILES['ufile']['type']; switch($type) { case "image/pjpeg" : $ok = 1; break; case "image/jpeg" : $ok = 1; break; case "image/png" : $ok = 1; break; case "image/gif" : $ok = 1; break; } if($ok||$error==0) { move_uploaded_file($tmp_name,$path.$fname); echo "<script>alert('文件上传成功!');</script>"; } */ //文件检验通过后上传: if(move_uploaded_file($tmp_name,$path.$fname)) { echo "文件上传成功!"; } else { echo "上传文件失败!"; } } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data"> <p>选择要上传的文件:<input type="file" name="ufile"></p> <p><input type="submit" name="submit" value="上传"></p> </form>