1 <form action="" enctype="multipart/form-data" method="post"> 2 上传文件:<input type="file" name="upload"><br/> 3 <input type="submit" value="提交"> 4 </form> 5 <?php 6 if(is_uploaded_file($_FILES['upload']['tmp_name'])) { 7 $upload = $_FILES["upload"]; 8 $name = $upload["name"];//上传文件的文件名 9 $types = $upload["type"];//上传文件的类型 10 $size = $upload["size"];//上传文件的大小 11 $tmp_name = $upload["tmp_name"];//上传文件的临时存放路径 12 if(!empty($name)){ 13 $file_types = explode(".", $name); 14 $file_type = $file_types[count($file_types)-1]; 15 if(strtolower($file_type)!="txt"){ 16 echo "不是txt文件,请重新上传!"; 17 } 18 $savePath = "E:\wamp\www\test\upload"; 19 $str = date('YMDhis'); 20 $file_name = $str . '.' . $file_type; 21 if (! copy ($tmp_name, $savePath.'\'.$file_name)){ 22 echo("failed"); 23 } 24 else{ 25 echo($savePath .'\'. $file_name); 26 echo("success<br>"); 27 } 28 } 29 } 30 ?>
PHP上传文件改进版:
1 <?php 2 if($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg" && ($_FILES["file"]["size"]) < 20000){ 3 if($_FILES["file"]["error"] > 0){ 4 echo "Return Code:". $_FILES["file"]["error"]; 5 }else{ 6 echo "Upload:" . $_FILES["file"]["name"]; 7 echo "Type:" . $_FILES["file"]["type"]; 8 echo "Size:" . ($_FILES["file"]["size"]/1024) . "Kb"; 9 echo "Temp file" . $_FILES["file"]["tmp_name"]; 10 if(file_exists("upload/".$_FILES["file"]["name"])){ 11 echo $_FILES["file"]["name"] . "already exists."; 12 }else{ 13 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$_FILES["file"]["name"]); 14 echo "Stored in:" . "upload/" . $_FILES["file"]["name"]; 15 } 16 } 17 }else{ 18 echo "Invalid file"; 19 } 20 ?>