- 论坛徽章:
- 0
|
功能:实现文件上传:
文件:index2.html upload.php
[root@localhost htdocs]# cat index2.html
<html>
<head>
<title>文件上传实例</title>
</head>
<body>
<h1>上传新文件</h1>
<form enctype="multipart/form-data" action="upload.php" method=post>
<input type="hidden" name="MAX_FILE_SIZE" value="100">
上传文件: <input name="userfile" type="file">
<input type="submit" value="上传">
</form>
</body>
</html>
=================================================
[root@localhost htdocs]# cat upload.php
<title>上传文件...</title>
</head>
<body>
<h1>上传文件...</h1>
<?php
if ($_FILES['userfile']['error'] > 0)
{
echo '上传错误: ';
switch ($_FILES['userfile']['error'])
{
case 1: echo '上传文件大小超出了就去干PHP配置文件中的约定值:upload_max_filesize'; break;
case 2: echo '上传文件大小超出了表单中的约定值: max_file_size'; break;
case 3: echo '文件只被部分上载'; break;
case 4: echo '没有上传任何文件'; break;
}
exit;
}
// Does the file have the right MIME type?
//此教程来源于97xxoo教程网(www.97xxoo.org)
//查看完整的教程请点:http://www.97xxoo.org/article/1/2008/20081109291.shtml
if ($_FILES['userfile']['type'] != 'text/plain')
{
echo '问题: 文件不是一个文本文件。';
exit;
}
// put the file where we'd like it
$upfile = $_FILES['userfile']['name'];
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) //判断是否为上传文件
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))//移动文件
{
echo '问题: 不能将文件移动到指定目录。';
exit;
}
}
else
{
echo '问题: 上传文件不是一个合法文件: ';
echo $_FILES['userfile']['name'];
exit;
}
echo 'File uploaded successfully<br><br>';
// reformat the file contents
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
// show what was uploaded
echo 'Preview of uploaded file contents:<br><hr>';
echo $contents;
echo '<br><hr>';
?>
</body>
</html>
=================================== |
|