php实现简单文件上传,代码如下: 1、upload.html <html> <head> <title>Adinistration - upload new files</title> </head> <body> <h1>Upload new news files</h1> <form action="upload.php" method="post" enctype="multipart/form-data"> <div> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <label for="userfile">Upload a file:</label> <input type="file" name="userfile" id="userfile" /> <input type="submit" value="Send file" /> </div> </form> </body> </html> 2、处理页:upload.php <html> <head> <title>Uploading...</title> </head> <body> <h1>Uploading file...</h1> <?php if($_FILES['userfile']['error']>0){ echo 'Problem:'; switch($_FILES['userfile']['error']){ case 1:echo 'File excedded upload_max_filesize'; break; case 2:echo 'File exceeded max_file_size'; break; case 3:echo 'File only partially uploaded'; break; case 4:echo 'No file uploaded'; break; case 6:echo 'Cannot upload file:No temp directory specified'; break; case 7:echo 'Upload failed:Cannot write to disk'; break; } exit; } // Does the file have the right MIME type? if($_FILES['userfile']['type']!='text/plain'){ echo 'Problem: file is not plain text'; exit; } //echo '$_FILES["userfile"]["tmp_name"]:'.$_FILES['userfile']['tmp_name'].'<br />'; // put the file where we'd like it $upfile='D:/usr_temp/php/uploads/'.$_FILES['userfile']['name']; echo '$upfile:'.$upfile."<br />"; if(is_uploaded_file($_FILES['userfile']['tmp_name'])){ if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)){ echo 'Problem: Could not move file to desination directory'; exit; } } else{ echo 'Problem : Possible file upload attack. Filename:'; echo $_FILES['userfile']['name']; exit; } echo 'File uploaded successfully<br><br>'; // remove possible HTML and PHP tags from the file's contents $contents=file_get_contents($upfile); $contents=strip_tags($contents); file_put_contents($_FILES['userfile']['name'],$contents); // show what was uploaded echo '<p>Preview of uploaded file contents:<br/><hr/>'; echo nl2br($contents); echo '<br/><hr/>'; ?> </body> </html>