本文分享一段php上传图片的代码,通过内置的php函数实现文件上传,有需要的朋友参考下。
1,form表单部分 <!-- Form Area --> <form enctype="multipart/form-data" action="uploader.php" method="post"> Select Image: <input type="file" name="userfile"> <input type="submit" value="Upload!"> </form> <!-- Form Area --> 로그인 후 복사 2,上传图片文件的php代码 <?php # Variables $path = "images/"; $max_size = "200000"; # File $filename = $_POST['userfile']; # Control if (!isset($HTTP_POST_FILES['userfile'])) exit; if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) { if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "文件太大,超过了上传文件的最大限制。The Max File Size is $max_size KB<br>n"; exit; } # Type Control if ( ($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/jpg") || ($HTTP_POST_FILES['userfile']['type']=="image/bmp") || ($HTTP_POST_FILES['userfile']['type']=="image/png") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") ) { # If File Exist if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "同名的文件已存在。<br>"; exit; } $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path . $HTTP_POST_FILES['userfile']['name']); if (!$res){ echo "上传失败!<br>"; exit; } else{ echo "上传成功!<br>"; } echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>"; echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>"; echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>"; echo "View Image"; } else { echo "错误的文件类型<br>"; exit; } } ?> 로그인 후 복사 注意: 在php5以后的代码,已经不再使用这样的方式,改用全局变量$_FILE来接收上传数据了。 |