1,html表单部分
<head>
<title>php文件上传-www.</title>
</head>
<body>
<h3>文件上传</h3>
选择上传文件:<br>
<form action="/blog_article/uploader.html" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="45">
<br>
<input type="submit" value="上传文件">
</form>
</body>
</html>
2,文件上传程序 uploader.php
<?php
/**
* 接收文件上传信息
* by www.
*/
if( $_FILES['file']['name'] != "" )
{
copy ( $_FILES['file']['tmp_name'],
"C:/Apache/htdocs/" . $_FILES['file']['name'] )
or die( "Could not copy file" );
}
else
{
die( "No file specified" );
}
?>
<html>
<head>
<title>上传成功</title>
</head>
<body>
<h3>文件上传成功...</h3>
<ul>
<li>文件名称: <?php echo $_FILES['file']['name']; ?></li>
<li>文件大小: <?php echo $_FILES['file']['size']; ?> bytes</li>
<li>文件类型: <?php echo $_FILES['file']['type']; ?></li>
</ul>
<a href="/blog_article/</php echo $_FILES[.html'file']['name']; ?>">点这里查看文件</a>
</body>
</html>
附:文件上传的错误代码
UPLOAD_ERR_INI_SIZE The uploaded file exceeds the maximum value specified in the php.ini file.
UPLOAD_ERR_FORM_SIZE The uploaded file exceeds the maximum value specified by the MAX_FILE_SIZE hidden widget.
UPLOAD_ERR_PARTIAL The file upload was canceled and only part of the file was uploaded.
UPLOAD_ERR_NOFILE No file was uploaded.
php文件上传代码分享,如下所示:
<html> <head> <title>文件上传脚本-www.</title> </head> <body> <div> <?php if ( isset( $_FILES['fupload'] ) ) { print "文件名称: ". $_FILES['fupload']['name'] ."<br />"; print "文件大小: ". $_FILES['fupload']['size'] ." bytes<br />"; print "临时文件名称: ".$_FILES['fupload']['tmp_name'] ."<br />"; print "文件类型: ". $_FILES['fupload']['type'] ."<br />"; print "错误消息: ". $_FILES['fupload']['error'] ."<br />"; if ( $_FILES['fupload']['type'] == "image/gif" ) { $source = $_FILES['fupload']['tmp_name']; $target = "upload/".$_FILES['fupload']['name']; move_uploaded_file( $source, $target );// or die ("Couldn't copy"); $size = getImageSize( $target ); $imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" "; $imgstr .= "src=/index.html"$target\" alt=\"uploaded image\" /></p>"; print $imgstr; } } ?> </div> <form enctype="multipart/form-data" action="/blog_article/</php print $_SERVER[.html'PHP_SELF']?>" method="post"> <p> <input type="hidden" name="MAX_FILE_SIZE" value="102400" /> <input type="file" name="fupload" /><br/> <input type="submit" value="上传文件" /> </p> </form> </body> </html>附:简单的文件上传表单
<html> <head> <title>简单的文件上传表单</title> </head> <body> <form enctype="multipart/form-data" action="/blog_article/</print $_SERVER[.html'PHP_SELF']?>" method="post"> <p> <input type="hidden" name="MAX_FILE_SIZE" value="102400" /> <input type="file" name="fupload" /><br/> <input type="submit" value="上传文件" /> </p> </form> </body> </html>
以上这个是最基础的文件上传表单,其中设置了最大上传文件的大小,通过$_SERVER['PHP_SELF']获得当前php文件名。
验证复选框的php代码,如下:
<?php /** * 在php中验证复选框的有效性 * by www. */ $value = 'yes'; echo "<input type='checkbox' name='subscribe' value='yes'/> 验证数据"; if (isset($_POST['subscribe'])) { if ($_POST['subscribe'] == $value) { $subscribed = true; } else { $subscribed = false; print '提交的复选框值无效。'; } } else { $subscribed = false; } if ($subscribed) { print '验证有效!'; } else { print '验证无效!'; }